简单的Android对文件进行读写操作
环境:1.系统:window7
2.Android版本:2.2
3.eclipse3.5.2(伽利略)
一、基本的流程图(写入文件)
二、模拟器运行界面
1.主界面
2.保存(不符合条件)
3.正常保存
4.读取文件
5.关于编者
三、代码如下:
1.保存文件的主要代码
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.读取文件的主要代码
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.关于编者的代码
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@gmail.com " + " \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
public void onClick(
DialogInterface dialog, int id) {
// TODO Auto-generated method stub
dialog.dismiss();
}
}).show();
}
});
4.业务代码
* 保存文件
*/
public static void save(OutputStream outStream, String content) throws Exception {
// 写入数据
outStream.write(content.getBytes());
outStream.close();
}
/*
* 读取文件
*/
public static String read(InputStream inStream) throws Exception {
// 字节流
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte [] buffer = new byte [ 1024 ];
int len = - 1 ;
// 获取字节数据
while ((len = inStream.read(buffer)) != - 1 ) {
outStream.write(buffer, 0 , len);
}
// 得到字节数据
byte [] data = outStream.toByteArray();
outStream.close();
inStream.close();
return new String(data);
}
四、小结
这个例子只是简单的操作文件的读写,很多地方都不够完善,希望有兴趣的网友们可以互相交流下~~~