Android 简易读写文件操作
- 格式:docx
- 大小:24.54 KB
- 文档页数:15
Android 简易读写文件操作(转)
代码如下:
1.保存文件的主要代码
保存的主代码
1 bSave.setOnClickListener(new View.OnClickListener() {
2
3 @Override
4 public void onClick(View v) {
5 // TODO Auto-generated method stub
6 resId=R.string.success;
7 file_name=filename.getText().toString();
8 file_content=filecontent.getText().toString();
9 /* openFileOutput API
10 * FileOutputStream openFileOutput (String name, int mode)
11 * Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist.
12 */
13 /* Context四种模式的API
14 * Context.MODE_APPEND -->if the file already exists then write data to the end of the existing file
instead of erasing it.
15 * Context.MODE_PRIVATE -->the default mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID).
16 * Context.MODE_WORLD_READABLE -->allow all other applications to have read access to the created file.
17 * Context.MODE_WORLD_WRITEABLE -->allow all other applications to have write access to the created file.
18 */
19 flag=InsertInfo();
20 if(flag){
21 try{
22 OutputStream outStream=FileStoreActivity.this.openFileOutput(file_name, MODE_PRIVATE);
23 FileService.save(outStream, file_content);
24
25 }catch(Exception e){
26 Log.e(TAG, e.toString());
27 resId=R.string.error;
28 }
29 Toast.makeText(FileStoreActivity.this, resId, Toast.LENGTH_LONG).show();
30 }
31 }
32
33
34 });
2.读取文件的主要代码
读取主代码
1 Button bRead=(Button)findViewById(R.id.read);
2 bRead.setOnClickListener(new View.OnClickListener() {
3
4 @Override
5 public void onClick(View v) {
6 // TODO Auto-generated method stub
7 //此时隐藏了文件内容框和文件内容的label
8 filecontent.setVisibility(View.GONE);
9 TextView content=(TextView)findViewById(R.id.content);
10 content.setVisibility(View.GONE);
11 InputStream inStream;
12 file_name=filename.getText().toString();
13
14 /* API
15 * FileInputStream openFileInput (String name)
16 * Open a private file associated with this Context's application package for reading.
17 */
18 try {
19 inStream = FileStoreActivity.this.openFileInput(file_name);
20 String context=FileService.read(inStream).toString();
21 Toast.makeText(FileStoreActivity.this, context, Toast.LENGTH_LONG).show();
22 } catch (Exception e) {
23 Log.e(TAG,e.toString());
24 resId=R.string.error;
25 }
26 }
27 });
3.关于编者的代码
关于编码代码
Button bAbout=(Button)findViewById(R.id.about);
bAbout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new AlertDialog.Builder(FileStoreActivity.this).setTitle("编者信息")
.setMessage(
"编者: naive_monk" + "\n"
+ "爱好:篮球、音乐、交友" +
"\n"
+ "Q Q:1271522052"
+ "\n"
+ "邮箱:summerdir@"
+ "\n"
+ "所在城市:广东惠州 " +
"\n")
.setCancelable(false).setPositiveButton("确定",
new DialogInterface.OnClickListener()
{
@Override
public void onClick(
DialogInterface dialog, int
id) {
// TODO Auto-generated method stub
dialog.dismiss();
}
}).setNegativeButton("退出",
new DialogInterface.OnClickListener()
{
@Override