博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android对文件的操作(简单的文件读取与写入)
阅读量:5093 次
发布时间:2019-06-13

本文共 4094 字,大约阅读时间需要 13 分钟。

简单的Android对文件进行读写操作

  环境:1.系统:window7

        2.Android版本:2.2

               3.eclipse3.5.2(伽利略)

一、基本的流程图(写入文件) 

  2011011813351857.png

二、模拟器运行界面 

  1.主界面              

  2011011813400791.png

  2.保存(不符合条件)

  2011011813434486.png

  3.正常保存

  2011011813443945.png

  4.读取文件

  2011011813451770.png

  5.关于编者

  2011011813454359.png

三、代码如下:

  

  1.保存文件的主要代码 

 

ContractedBlock.gif
ExpandedBlockStart.gif
保存的主代码
 
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.读取文件的主要代码

 

ContractedBlock.gif
ExpandedBlockStart.gif
读取主代码
 
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.关于编者的代码

 

 

ContractedBlock.gif
ExpandedBlockStart.gif
关于编码代码
 
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@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.业务代码

 

 

ContractedBlock.gif
ExpandedBlockStart.gif
代码
 
/*
* 保存文件
*/
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);
}

 

 

四、小结

  

这个例子只是简单的操作文件的读写,很多地方都不够完善,希望有兴趣的网友们可以互相交流下~~~ 

 

 

 

 

 

转载于:https://www.cnblogs.com/maoan/archive/2011/01/18/1938278.html

你可能感兴趣的文章
solr后台操作Documents之增删改查
查看>>
http://yusi123.com/
查看>>
文件文本的操作
查看>>
Ubuntu linux下gcc版本切换
查看>>
记一次Web服务的性能调优
查看>>
jQuery.form.js使用
查看>>
(转)linux sort,uniq,cut,wc命令详解
查看>>
关于ExecuteNonQuery执行的返回值(SQL语句、存储过程)
查看>>
UVa540 Team Queue(队列queue)
查看>>
mysql数据增删改查
查看>>
akka之种子节点
查看>>
不知道做什么时
查看>>
matlab 给某一列乘上一个系数
查看>>
密码学笔记——培根密码
查看>>
Screening technology proved cost effective deal
查看>>
MAC 上升级python为最新版本
查看>>
创业老板不能犯的十种错误
查看>>
Animations介绍及实例
查看>>
判断请求是否为ajax请求
查看>>
【POJ2699】The Maximum Number of Strong Kings(网络流)
查看>>