MainActivity .java
package com.example.debug;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
String filename="test";
private EditText et;
private TextView show;
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.readBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
File myfile=new File(sdcard, "Thismyfile.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 (FileNotFoundException e) {
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
}
}
}
});
findViewById(R.id.writeBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
File myfile=new File(sdcard, "Thismyfile.txt");
if(!sdcard.exists()){
Toast.makeText(MainActivity.this, "当前设备不存在sd卡", Toast.LENGTH_LONG).show();
return;
}
try {
myfile.createNewFile();
Toast.makeText(MainActivity.this, "文件创建完成", Toast.LENGTH_LONG).show();
FileOutputStream fos=new FileOutputStream(myfile);
OutputStreamWriter osw=new OutputStreamWriter(fos);
osw.write(et.getText().toString());
osw.flush();
osw.close();
fos.close();
Toast.makeText(MainActivity.this, "文件已经写入完成", Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
activity_main.xml:
<LinearLayout 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"
tools:context=".MainActivity" >
<EditText
android:id="@+id/et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="在这里输入要保存的数据"
/>
<Button
android:id="@+id/writeBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="写入数据" />
<Button
android:id="@+id/readBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="读取数据" />
<TextView
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="这里显示读到的内容"
/>
</LinearLayout>
添加权限: