您的位置:首页 > 移动开发 > Android开发

Android中的文件存储数据方式

2011-07-19 17:11 519 查看
1.文件存储数据使用了Java中的IO操作来进行文件的保存和读取,只不过Android在Context类中封装好了输入流和输出流的获取方法。

创建的存储文件保存在/data/data/<package name>/files文件夹下。





2.操作。

保存文件内容:通过Context.openFileOutput获取输出流,参数分别为文件名和存储模式。

读取文件内容:通过Context.openFileInput获取输入流,参数为文件名。

删除文件:Context.deleteFile删除指定的文件,参数为将要删除的文件的名称。

获取文件名列表:通过Context.fileList获取files目录下的所有文件名数组。

*获取文件路径的方法:

绝对路径:/data/data/<package name>/files/filename

Context:Context.getFilesDir()可以获取到”/data/data/<package name>/files”

3.四种文件保存的模式。

Context.MODE_PRIVATE 为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下写入的内容会覆盖原文件的内容。

Context.MODE_APPEND 检查文件是否存在,存在就往文件追加内容,否则就创建新文件。

MODE_WORLD_READABLE 表示当前文件可以被其他应用读取。

MODE_WORLD_WRITEABLE 表示当前文件可以被其他应用写入。

在使用模式时,可以用”+”来选择多种模式,比如openFileOutput(FILENAME, Context.MODE_PRIVATE + MODE_WORLD_READABLE);

下面通过程序来演示下文件存储的使用。完整代码下载:

点击链接下载android_files

大小 : 44.29 kB

下载次数 : 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

/** 
 * MainActivity 
 *  
 * @author zuolongsnail 
 *  
 */  
 public class MainActivity extends Activity {  
 private EditText writeET;  
 private Button writeBtn;  
 private TextView contentView;  
 public static final String FILENAME = "setting.set";  
 
 @Override  
 public void onCreate(Bundle savedInstanceState) {  
 super.onCreate(savedInstanceState);  
 setContentView(R.layout.main);  
 writeET = (EditText) findViewById(R.id.write_et);  
 writeBtn = (Button) findViewById(R.id.write_btn);  
 contentView = (TextView) findViewById(R.id.contentview);  
 writeBtn.setOnClickListener(new OperateOnClickListener());  
 }  
 
 class OperateOnClickListener implements OnClickListener {  
 @Override  
 public void onClick(View v) {  
 writeFiles(writeET.getText().toString());  
 contentView.setText(readFiles());  
 System.out.println(getFilesDir());  
 }  
 }  
 
 // 保存文件内容  
 private void writeFiles(String content) {  
 try {  
 // 打开文件获取输出流,文件不存在则自动创建  
 FileOutputStream fos = openFileOutput(FILENAME,  
 Context.MODE_PRIVATE);  
 fos.write(content.getBytes());  
 fos.close();  
 } catch (Exception e) {  
 e.printStackTrace();  
 }  
 }  
 
 // 读取文件内容  
 private String readFiles() {  
 String content = null;  
 try {  
 FileInputStream fis = openFileInput(FILENAME);  
 ByteArrayOutputStream baos = new ByteArrayOutputStream();  
 byte[] buffer = new byte[1024];  
 int len = 0;  
 while ((len = fis.read(buffer)) != -1) {  
 baos.write(buffer, 0, len);  
 }  
 content = baos.toString();  
 fis.close();  
 baos.close();  
 } catch (Exception e) {  
 e.printStackTrace();  
 }  
 return content;  
 }  
 }

程序截图:





提供一个文件存储数据的工具类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

/** 
 * 文件存储数据方式工具类 
 *  
 * @author zuolongsnail 
 */  
 public class FilesUtil {  
 /** 
 * 保存文件内容 
 *  
 * @param c 
 * @param fileName 
 *            文件名称 
 * @param content 
 *            内容 
 */  
 private void writeFiles(Context c, String fileName, String content, int mode)  
 throws Exception {  
 // 打开文件获取输出流,文件不存在则自动创建  
 FileOutputStream fos = c.openFileOutput(fileName, mode);  
 fos.write(content.getBytes());  
 fos.close();  
 }  
 
 /** 
 * 读取文件内容 
 *  
 * @param c 
 * @param fileName 
 *            文件名称 
 * @return 返回文件内容 
 */  
 private String readFiles(Context c, String fileName) throws Exception {  
 ByteArrayOutputStream baos = new ByteArrayOutputStream();  
 FileInputStream fis = c.openFileInput(fileName);  
 byte[] buffer = new byte[1024];  
 int len = 0;  
 while ((len = fis.read(buffer)) != -1) {  
 baos.write(buffer, 0, len);  
 }  
 String content = baos.toString();  
 fis.close();  
 baos.close();  
 return content;  
 }  
 }

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐