国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android存儲字符串數據到txt文件

2019-12-12 00:28:35
字體:
來源:轉載
供稿:網友

需求:

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"));

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阜宁县| 吴堡县| 特克斯县| 保靖县| 肇庆市| 赣州市| 吴桥县| 和林格尔县| 郧西县| 友谊县| 安泽县| 新巴尔虎左旗| 广饶县| 巨鹿县| 威信县| 华池县| 荆门市| 儋州市| 井研县| 长白| 彰武县| 衡水市| 中阳县| 新绛县| 石楼县| 怀来县| 唐海县| 屏山县| 金溪县| 舞阳县| 湘潭县| 钟祥市| 旅游| 米易县| 吴桥县| 翁牛特旗| 如东县| 孙吴县| 呼玛县| 松潘县| 来凤县|