Android数据持久化之File机制分析
作者:android小猪 时间:2021-05-29 19:24:56
本文实例讲述了Android数据持久化之File机制。分享给大家供大家参考,具体如下:
在使用Java SE平台开发C/S结构的软件中,File 的IO输入输出流的使用率是非常高的,通过使用IO输入输出流可以对存储介质上的文件进行读写操作,下面的代码就是实现一个在Android平台上使用File对象操作文件的功能:
package com.example.data_file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
@SuppressLint("SdCardPath")
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
File file = new File("/data/data/com.example.data_file/file.txt");
Log.v("path=", file.getAbsolutePath());
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
fos.write("乌拉啦啦啦".getBytes());
fos.close();
FileInputStream fis = new FileInputStream(file);
InputStreamReader isReader = new InputStreamReader(fis);
char[] charArray = new char[2];
int readLength = isReader.read(charArray);
StringBuffer sb = new StringBuffer();
while(readLength != -1)
{
sb.append(charArray, 0, readLength);
readLength = isReader.read(charArray);
}
Log.v("file.txt文件内容是:", sb.toString());
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
注意:Linux系统下对权限的要求比较高,不允许随便存储文件,所以存储文件的路径为:/data/data/<包名>/Files/<文件名>
希望本文所述对大家Android程序设计有所帮助。
标签:Android,数据持久化,File
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
android使用flutter的ListView实现滚动列表的示例代码
2023-06-26 09:00:13
![](https://img.aspxhome.com/file/2023/8/97858_0s.gif)
Mybatis拦截器的实现介绍
2023-07-04 04:23:31
![](https://img.aspxhome.com/file/2023/2/73082_0s.png)
AndroidManifest.xml中含盖的安全问题详解
2023-05-19 09:22:30
Java日常练习题,每天进步一点点(33)
2023-09-22 05:32:41
C# Socket数据接收的三种实现方式
2022-04-17 17:22:01
![](https://img.aspxhome.com/file/2023/7/98157_0s.png)
Java程序员必须知道的5个JVM命令行标志
2023-11-11 15:30:36
Spring Boot2.3 新特性分层JAR的使用
2021-08-03 12:55:50
![](https://img.aspxhome.com/file/2023/2/65602_0s.jpg)
Java 数据库连接池详解及简单实例
2023-08-10 15:54:07
Java将json对象转换为map键值对案例详解
2023-11-28 07:38:43
![](https://img.aspxhome.com/file/2023/9/60129_0s.png)
Spring学习之Bean的装配多种方法
2023-09-04 09:32:36
图解JVM垃圾内存回收算法
2023-10-13 17:24:35
![](https://img.aspxhome.com/file/2023/4/64804_0s.jpg)
详解Java的引用类型及使用场景
2021-06-27 11:34:18
java利用jacob将word转pdf
2023-02-05 08:23:03
![](https://img.aspxhome.com/file/2023/0/75510_0s.jpg)
SpringBoot+Elasticsearch实现数据搜索的方法详解
2023-04-17 02:45:46
Spring实战之使用@Resource配置依赖操作示例
2023-08-06 10:11:13
Java算法之最长公共子序列问题(LCS)实例分析
2022-12-06 08:31:42
![](https://img.aspxhome.com/file/2023/8/101918_0s.jpg)
C#缩略图多路径多格式保存的实例
2021-10-29 18:12:36
java实现多人聊天室可视化
2021-08-27 01:16:49
![](https://img.aspxhome.com/file/2023/0/79180_0s.jpg)
Java使用Redis实现秒杀功能
2023-04-11 11:26:54
![](https://img.aspxhome.com/file/2023/1/78001_0s.jpg)
基于Java8实现提高Excel读写效率
2023-11-25 10:01:37