package com.powenko.Tutorial_File_read_write; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.util.Calendar; import java.util.Date; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class Tutorial_File_read_write extends Activity { /** Called when the activity is first created. */ protected String filePath="myfilename.txt"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); file_write(filePath,"PowenKo.com sample code"); try { file_read(filePath); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 時間有問題 File file = new File(filePath); Date lastModDate = new Date(file.lastModified()); Log.i("powen:","File last modified: "+ lastModDate.toString()); long dateModified=file.lastModified(); Date date = new Date(dateModified*1000); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); String timeline = String.format("%1$4d" + "-" + "%2$02d",calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1); } protected void file_read(String i_filePath) throws IOException{ // try opening the myfilename.txt String line; try { // open the file for reading InputStream instream = openFileInput(i_filePath); // if file the available for reading if (instream!=null) { // prepare the file for reading InputStreamReader inputreader = new InputStreamReader(instream); BufferedReader buffreader = new BufferedReader(inputreader); StringBuffer stringBuffer = new StringBuffer(""); // line =buffreader.readLine(); char[] buffer = new char[3072]; int len1 = 0; while ( (len1 = buffreader.read(buffer) ) != -1 ) { String buff = new String(buffer,0,len1); stringBuffer.append(buff); } String stranica = new String(stringBuffer); // c.disconnect(); buffreader.close(); } // close the file again // instream.close(); } catch (java.io.FileNotFoundException e) { // do something if the myfilename.txt does not exits } } protected void file_write(String i_filePath,String mySettings){ // try to write the content try { // open myfilename.txt for writing OutputStreamWriter out = new OutputStreamWriter(openFileOutput(i_filePath,0)); // write the contents on mySettings to the file out.write(mySettings); // close the file out.close(); } catch (java.io.IOException e) { //do something if an IOException occurs. } } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.powenko.Tutorial_File_read_write" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Tutorial_File_read_write" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
sample code:
Tutorial_File_read_write