存取手机中的文件数据。
写入和读取的操作格式均为UTF-8。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private String filename = "test";
private TextView show;
private EditText et;
File sdcard = Environment.getExternalStorageDirectory();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.et);
show = (TextView) findViewById(R.id.show);
findViewById(R.id.writeBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
osw.write(et.getText().toString());
osw.flush();
fos.flush();
osw.close();
fos.close();
Toast.makeText(getApplicationContext(), "写入完成", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
findViewById(R.id.readBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
FileInputStream fis = openFileInput(filename);
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
char input[] = new char[fis.available()];
isr.read(input);
isr.close();
fis.close();
String readed = new String(input);
show.setText(readed);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
findViewById(R.id.writeBtn1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//首先要获得当前sd卡的工作目录
File myfile = new File(sdcard,"This is my file.txt");
if(!sdcard.exists()){
Toast.makeText(getApplicationContext(), "there is no sdcard", Toast.LENGTH_SHORT).show();
return;
}
try {
myfile.createNewFile();
FileOutputStream fos = new FileOutputStream(myfile);
OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
osw.write(et.getText().toString());
osw.flush();
osw.close();
fos.close();
Toast.makeText(getApplicationContext(), "文件已创建完成", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
findViewById(R.id.readBtn1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File myfile = new File(sdcard,"This is my file.txt");
if(myfile.exists()){
try {
FileInputStream fis = new FileInputStream(myfile);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
char[] input = new char[fis.available()];
isr.read(input);
isr.close();
fis.close();
String inString = new String(input);
show.setText(inString);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
}
布局文件:
放入1个EditText,4个button,1个textview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.rust.readfiletest.MainActivity" >
<EditText
android:id="@+id/et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="enter" />
<Button
android:id="@+id/writeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et"
android:text="save" />
<Button
android:id="@+id/readBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et"
android:layout_toRightOf="@id/writeBtn"
android:text="read" />
<Button
android:id="@+id/writeBtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et"
android:layout_toRightOf="@id/readBtn"
android:text="write e" />
<Button
android:id="@+id/readBtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et"
android:layout_toRightOf="@id/writeBtn1"
android:text="read e" />
<TextView
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/writeBtn"
/>
</RelativeLayout>