Android存儲(chǔ)字符串?dāng)?shù)據(jù)到txt文件
需求:
android存儲(chǔ)字符串?dāng)?shù)據(jù)簡單的有SharePerfence不過只能存儲(chǔ)89kb最多的數(shù)據(jù)(好像),超過這個(gè)數(shù)據(jù)如果不方便網(wǎng)絡(luò)存儲(chǔ),只能用文件存儲(chǔ)了,這里寫了一個(gè)工具類,存儲(chǔ)到txt文件(不重要的數(shù)據(jù),但是體量大)
代碼:
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) {
//生成文件夾之后,再生成文件,不然會(huì)出錯(cuò)
makeFilePath(filePath, fileName);
String strFilePath = filePath + fileName;
// 每次寫入時(shí),都換行寫
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文件的文件內(nèi)容
public static String getFileContent(File file) {
String content = "";
if (!file.isDirectory()) { //檢查此路徑名的文件是否是一個(gè)目錄(文件夾)
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();//關(guān)閉輸入流
}
} 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、調(diào)用 - 寫入
FileUtils.writeTxtToFile(idPASideBase64, "/sdcard/Gyt/", "idPASide.txt");
3、調(diào)用 - 讀取
String idPASideBase64 = FileUtils.getFileContent(new File("/sdcard/Gyt/idPASide.txt"));
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)保存QQ賬號(hào)與密碼功能(文件存儲(chǔ))
- Android開發(fā)文件存儲(chǔ)實(shí)例
- Android 文件存儲(chǔ)與SharedPreferences存儲(chǔ)方式詳解用法
- 淺析Android文件存儲(chǔ)
- android I/0流操作文件(文件存儲(chǔ))
- 詳解Android 中的文件存儲(chǔ)
- Android圖片添加水印圖片并把圖片保存到文件存儲(chǔ)的實(shí)現(xiàn)代碼
- Android開發(fā)實(shí)現(xiàn)讀取Assets下文件及文件寫入存儲(chǔ)卡的方法
- Android?文件存儲(chǔ)系統(tǒng)原理
相關(guān)文章
Android 自定義標(biāo)題欄 顯示網(wǎng)頁加載進(jìn)度的方法實(shí)例
Android 自定義標(biāo)題欄 顯示網(wǎng)頁加載進(jìn)度的方法實(shí)例,需要的朋友可以參考一下2013-06-06
Android手機(jī)上同時(shí)安裝正式包與測試包的方法
這篇文章主要給大家介紹了關(guān)于Android手機(jī)上同時(shí)安裝正式包與測試包的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
Android Studio 新手入門教程(一)基本設(shè)置圖解
這篇文章主要介紹了Android Studio 新手入門教程(一)基本設(shè)置圖解,需要的朋友可以參考下2017-12-12
Android實(shí)現(xiàn)毛玻璃效果彈出菜單動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)毛玻璃效果彈出菜單動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Android中View跟隨手指滑動(dòng)效果的實(shí)例代碼
這篇文章主要介紹了Android中View跟隨手指滑動(dòng)效果的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
Android使用ImageView 制作透明圓弧實(shí)例代碼
這篇文章主要介紹了Android使用ImageView 制作透明圓弧實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-05-05

