Android存储字符串数据到txt文件
作者:zst1303939801 时间:2021-11-05 21:55:16
需求:
android存储字符串数据简单的有SharePerfence不过只能存储89kb最多的数据(好像),超过这个数据如果不方便网络存储,只能用文件存储了,这里写了一个工具类,存储到txt文件(不重要的数据,但是体量大)
代码:
1、工具类
package com.xxx.util;
import android.os.Environment;
import android.util.Log;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
/**
* 文件工具类
* Created by zst on 2018/2/1.
*/
public class FileUtils {
// 将字符串写入到文本文件中
public static void writeTxtToFile(String strcontent, String filePath, String fileName) {
//生成文件夹之后,再生成文件,不然会出错
makeFilePath(filePath, fileName);
String strFilePath = filePath + fileName;
// 每次写入时,都换行写
String strContent = strcontent + "\r\n";
try {
File file = new File(strFilePath);
if (!file.exists()) {
Log.d("TestFile", "Create the file:" + strFilePath);
file.getParentFile().mkdirs();
file.createNewFile();
}
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
raf.seek(file.length());
raf.write(strContent.getBytes());
raf.close();
} catch (Exception e) {
Log.e("TestFile", "Error on write File:" + e);
}
}
//生成文件
public static File makeFilePath(String filePath, String fileName) {
File file = null;
makeRootDirectory(filePath);
try {
file = new File(filePath + fileName);
if (!file.exists()) {
file.createNewFile();
}
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
//生成文件夹
public static void makeRootDirectory(String filePath) {
File file = null;
try {
file = new File(filePath);
if (!file.exists()) {
file.mkdir();
}
} catch (Exception e) {
Log.i("error:", e + "");
}
}
//读取指定目录下的所有TXT文件的文件内容
public static String getFileContent(File file) {
String content = "";
if (!file.isDirectory()) { //检查此路径名的文件是否是一个目录(文件夹)
if (file.getName().endsWith("txt")) {//文件格式为""文件
try {
InputStream instream = new FileInputStream(file);
if (instream != null) {
InputStreamReader inputreader
= new InputStreamReader(instream, "UTF-8");
BufferedReader buffreader = new BufferedReader(inputreader);
String line = "";
//分行读取
while ((line = buffreader.readLine()) != null) {
content += line + "\n";
}
instream.close();//关闭输入流
}
} catch (java.io.FileNotFoundException e) {
Log.d("TestFile", "The File doesn't not exist.");
} catch (IOException e) {
Log.d("TestFile", e.getMessage());
}
}
}
return content;
}
}
2、调用 - 写入
FileUtils.writeTxtToFile(idPASideBase64, "/sdcard/Gyt/", "idPASide.txt");
3、调用 - 读取
String idPASideBase64 = FileUtils.getFileContent(new File("/sdcard/Gyt/idPASide.txt"));
来源:https://blog.csdn.net/u012246458/article/details/83063112
标签:Android,存储,字符串
0
投稿
猜你喜欢
java实现socket客户端连接服务端
2021-12-02 03:52:07
Java使用sftp定时下载文件的示例代码
2022-11-14 06:11:47
史上最全图文讲解Java泛型
2022-08-23 20:27:47
Java详细讲解不同版本的接口语法和抽象类与接口的区别
2022-09-30 01:46:38
Java数据结构与算法之稀疏数组与队列深入理解
2022-05-31 06:33:11
C# 实现TXT文档转Table的示例代码
2022-04-23 07:47:52
java字符串抉择
2023-08-08 14:20:05
Java Spring之@Async原理案例详解
2023-05-31 17:13:15
SpringBoot结合JSR303对前端数据进行校验的示例代码
2022-09-15 03:22:55
Android 5秒学会使用手势解锁功能
2023-07-11 13:48:32
java实现哈弗曼编码与反编码实例分享(哈弗曼算法)
2023-11-25 04:54:05
基于java构造方法Vector删除元素源码分析
2023-11-25 14:54:45
Java 数据结构与算法系列精讲之栈
2021-06-11 09:00:51
android Matrix实现图片随意放大缩小或拖动
2022-02-12 12:06:48
Eclipse中安装反编译工具Fernflower的方法(Enhanced Class Decompiler)
2021-09-12 09:01:13
常见的java面试题
2023-11-26 18:03:44
java二叉树面试题详解
2021-06-13 08:40:23
浅谈mybatis中SQL语句给boolean类型赋值问题
2023-01-19 15:15:42
Spring实战之ResourceLoader接口资源加载用法示例
2023-11-20 12:16:49
Spring @Profile注解详解
2023-04-20 06:26:16