android数据写入文件格式,Android 本地文件保存数据(2020-08-07)
文件操作需要手机权限,需要在AndroidManifest.xml添加
--------------------------------第一种方式------------------------------------------
1、获取文件路径
/**
* 获取文件路径
* filePath=/storage/emulated/0/Android/data/com.example.wifiapplication/files/20120101_100216_log.txt
* */
public String getFilesPath(Context context) {
// 20120101_100216_log.txt 用时间格式创建文件名
Calendar c = Calendar.getInstance();
SimpleDateFormat fdate = new SimpleDateFormat("yyyyMMdd_HHmmss");
String fname = fdate.format(c.getTime()) + "_log.txt";
String filePath ;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) {
//外部存储可用
filePath = context.getExternalFilesDir(null).getPath() + "/" + fname;
} else {
//外部存储不可用
filePath = context.getFilesDir().getPath() + "/" + fname;
}
return filePath;
}
2、写入数据
try {
FileOutputStream outputStream = new FileOutputStream(filePath, true);// true 为 追加写入,false 为删除写入(把之前的数据清除掉,再写入)
outputStream.write("88888".getBytes());
outputStream.write("99999999".getBytes());
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
3、读取数据
try {
FileInputStream inputStream = new FileInputStream(filePath);
byte[] buffer = new byte[1024];
int hasRead = inputStream.read(buffer);
StringBuilder sb = new StringBuilder();
sb.append(new String(buffer, 0, hasRead));
Log.e("wyy", "读取数据=" + sb.toString());
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
--------------------------------第二种方式------------------------------------------
1、获取名称
/**
* 获取文件名称
* */
private String getFileName(){
Calendar c = Calendar.getInstance();
SimpleDateFormat fdate = new SimpleDateFormat("yyyyMMdd_HHmmss");
return fdate.format(c.getTime()) + "_log.txt";
}
2、写入数据
try {
FileOutputStream outputStream = FileHandleActivity.this.openFileOutput(getFileName(),Context.MODE_APPEND);
outputStream.write("需要写入的数据".getBytes());
outputStream.close();// 关闭文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
3、读取数据
try {
FileInputStream inputStream = this.openFileInput(fname);
byte[] buffer = new byte[1024];
int hasRead = inputStream.read(buffer);
StringBuilder sb = new StringBuilder();
sb.append(new String(buffer, 0, hasRead));
Log.e("wyy", "读取数据=" + sb.toString());
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
【注意】第一种方法和第二种方法的区别:
1.文件路径不一样
2.第一种方法可能对API要求比较高,亲测在minSdkVersion 19 / targetSdkVersion 29上是没有问题。但是不保证在你的手机上没有问题。具体情况具体分析。
android数据写入文件格式,Android 本地文件保存数据(2020-08-07)相关推荐
- Jsoup:使用Java将爬虫得到的数据写入Excel,Jsoup得到的数据进行持久化,爬虫数据保存到本地Excel中
Jsoup:使用Java将爬虫得到的数据写入Excel,Jsoup得到的数据进行持久化,爬虫数据保存到本地Excel中 一.资源 EasyExcel使用教程 Jsoup爬虫教程 二.代码 xml依赖 ...
- java接收并存储文件_客户端读取本地文件的数据,发送到服务器,服务器接收并存储到文件中...
只实现了传送文件的功能. 编写Socket客户端和服务器程序,客户端读取本地文件的数据,发送到服务器,服务器接收并存储到文件中. 消息格式: 字段 长度(单位字节) 内容 -------------- ...
- python 向已有excel写数据_将数据写入已有的excel文件并制作汇总页签
我必须将一些数据写入现有的xls文件中.(我应该说,我在unix上工作,不能使用windows) 我更喜欢使用python,并尝试过xlwt.openpyxl.xlutils等库.在 它不工作,因为在 ...
- Spark读取Hive数据的两种方式与保存数据到HDFS
Spark读取Hive数据的两种方式与保存数据到HDFS Spark读取Hive数据的方式主要有两种 1. 通过访问hive metastore的方式,这种方式通过访问hive的metastore元数 ...
- Android 将MAP格式数据写入XML 将XMP文件读MAP数据格式中
其中涉及的部分类可以自行查询,如:AtomicFile.java FastXmlSerializer.java private static final String APPCONFIG_FILE ...
- 将android程序写入内存,android中将数据写入手机内存和sdcard中的文件
在android中一般有两种方式: 第一种是写入手机内存的文件,这种方式,文件的存取路径都是在应用包下的 files 文件夹下,其路径是固定的,并不受程序员控制的, FileOutputStream ...
- android 图片 写入文件格式,怎么把web服务器端获取的图片写入android客户端文件...
怎么把web服务器端数据库获取的图片写入android客户端文件 解决方案 10 获得图片,然后用FileoutputStream写入到文件-- 10 先用htturlconnection(或其他方法 ...
- [ Android 五种数据存储方式之二 ] —— 文件存储数据
关于文件存储,Activity提供了openFileOutput()方法可以用于把数据输出到文件中,具体的实现过程与在J2SE环境中保存数据到文件中是一样的. 文件可用来存放大量数据,如文本.图片.音 ...
- Android数据存储之SharedPreferencesSave存储(保存数据,读取数据的操作)
GitHub项目地址: https://github.com/Skymqq/SharedPreferencesSave.git 不同于文件的存储方式,SharedPreferences是使用键值对的方 ...
最新文章
- oracle ORA-01000: maximum open cursors exceeded问题的解决方法
- [转载]MongoDB开发学习 经典入门
- Office远程代码执行漏洞补丁(905413)
- K8S部署工具:KubeOperator集群部署
- 数据的gzip压缩解压缩_使用GZIP和压缩数据
- Linux内核权限维持,Linux权限维持笔记
- 作者:刘诗凯(1983-),男,华为大数据分析产品部主任工程师。
- macOS中安装docker
- java 容器 List
- java调用opencv人脸识别 error insufficient out of memory
- 关于WINDOWS超级终端的使用来调试MODEM 串口
- 体细胞选择区分癌基因和抑癌基因
- Error: Getter not found: 'suspending'. case AppLifecycleState.suspending
- Access数据库,以及一些其他的库。
- 360的困兽之斗——探讨奇虎Tecent商业模式
- 电脑上怎么调出输入法
- UEBA案例分析系列之检测失陷凭证
- LLDP协议、STP协议 笔记
- 汉字机内码简介及转换工具
- 面向对象程序有哪些优点呢?