public class MainActivity extends Activity implements OnClickListener{
private Button btn_readout,btn_readin;
private EditText text_read,text_name;
private TextView view_read;
private FileOutputStream out=null;
private String name,conte;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init(){
btn_readout=(Button) findViewById(R.id.btn_readout);
btn_readin=(Button) findViewById(R.id.btn_readin);
text_read= (EditText) findViewById(R.id.text_read);
text_name= (EditText) findViewById(R.id.text_name);
view_read= (TextView) findViewById(R.id.view_read);// 用来显示读取的内容
btn_readout.setOnClickListener(this);
btn_readin.setOnClickListener(this);
}
public void write(String name,String conte ){
try {
out=openFileOutput(name, this.MODE_APPEND);
out.write(conte.getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String read(){
FileInputStream in=null;
StringBuilder sb=null;
try {
in=openFileInput(text_name.getText().toString());
sb=new StringBuilder();
byte[]bytes=new byte[1024];
int i=0;
while((i=in.read(bytes))!=-1){
sb.append(new String(bytes, 0, i));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb.toString();
}
//将文件写入sd卡
public void writeSD(String name,String conte){
//获取sd卡根目录+文件名得到File对象
File file=new File(Environment.getExternalStorageDirectory()+"/"+name+".txt");
try {
FileOutputStream fileout=new FileOutputStream(file);
fileout.write(conte.getBytes());
fileout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//从sd卡读取文件
public String readSD(String name){
//要读取的文件路径及文件名
File file=new File(Environment.getExternalStorageDirectory()+"/"+name+".text");
StringBuilder sb=null;
try {
FileInputStream in=new FileInputStream(file);
byte []bytes=new byte[1024];
int i=0;
sb=new StringBuilder();
while((i=in.read())!=-1){
sb.append(new String(bytes,0,i));
}
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb.toString();
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.btn_readin:
name=text_name.getText().toString().trim();
conte=text_read.getText().toString().trim();
write(name,conte);
Toast.makeText(MainActivity.this, "写入成功", 1000).show();
break;
case R.id.btn_readout:
name=text_name.getText().toString().trim();
view_read.setText(read());
break;
}
}
}

2780

被折叠的 条评论
为什么被折叠?



