当前位置: 移动技术网 > IT编程>移动开发>Android > Android存储字符串数据到txt文件

Android存储字符串数据到txt文件

2019年08月01日  | 移动技术网IT编程  | 我要评论

三支一扶报名入口,飙风铁马,佐藤佳代

需求:

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网