PowenKo, Android Tutorial 65-5,File sd card file read and write



main.xml


sample code: Tutorial_File_read_write_sdcard
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         android:id="@+id/result"
        android:text="@string/hello" />

</LinearLayout>
package com.powenko;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class Tutorial_File_read_write_sdcardActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		String eol = System.getProperty("line.separator");
		try {
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
					openFileOutput("myfile", MODE_WORLD_WRITEABLE)));
			writer.write("This is a test1." + eol);
			writer.write("This is a test2." + eol);
			writer.write("This is a test3." + eol);
			writer.write("This is a test4." + eol);
			writer.write("This is a test5." + eol);
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			BufferedReader input = new BufferedReader(new InputStreamReader(
					openFileInput("myfile")));
			String line;
			StringBuffer buffer = new StringBuffer();
			while ((line = input.readLine()) != null) {
				buffer.append(line + eol);
			}
			TextView textView = (TextView) findViewById(R.id.result);
			if (textView == null) {
				Log.e("TEST", "Wtf");
			}
			textView.setText(buffer.toString());

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}

<blockquote>

solution 2

</pre>
package com.TutorialFileReadWrite;

&nbsp;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

&nbsp;

import android.app.Activity;

import android.content.Context;

import android.os.Bundle;

import android.os.Environment;

import android.util.Log;

import android.widget.TextView;

&nbsp;

&nbsp;

&nbsp;

public class TutorialFileReadWrite extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Context context=getApplicationContext();

write( context,"test.txt", "How are you");

write_sd("/powenko" ,"sd.txt", "How are you");

String t1=read(context, "test.txt");

TextView mTextView01=(TextView) findViewById(R.id.TextView01);

mTextView01.setText(t1);

}

public static String read(Context context, String file) {

String data = "";

try {

FileInputStream stream = context.openFileInput(file);

StringBuffer sb = new StringBuffer();

int c;

while ((c = stream.read()) != -1) {

sb.append((char) c);

}

stream.close();

data = sb.toString();

} catch (FileNotFoundException e) {

} catch (IOException e) {

}

return data;

}

protected void write_sd(String i_filePath,String i_FileName,String msg){

// try to write the content

try {

File sdCard = Environment.getExternalStorageDirectory();

File dir = new File (sdCard.getAbsolutePath() + i_filePath);

dir.mkdirs();

File file = new File(dir,i_FileName);

FileOutputStream out = new FileOutputStream(file);

// write the contents on mySettings to the file

out.write(msg.getBytes());

// close the file

out.close();

} catch (java.io.IOException e) {

//do something if an IOException occurs.

}

}

public static void write(Context context, String file, String msg) {

try {

FileOutputStream stream = context.openFileOutput(file,

Context.MODE_WORLD_WRITEABLE);

stream.write(msg.getBytes());

stream.flush();

stream.close();

} catch (FileNotFoundException e) {

} catch (IOException e) {

}

}

&nbsp;

&nbsp;

}
<pre>

 


<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.TutorialFileReadWrite"

android:versionCode="1"

android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".TutorialFileReadWrite"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

&nbsp;

</application>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-sdk android:minSdkVersion="3" />

&nbsp;

</manifest>


Leave a Reply