温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在Android中将字符串数据存储到txt文件中

发布时间:2021-03-01 16:28:53 来源:亿速云 阅读:173 作者:戴恩恩 栏目:移动开发

本文章向大家介绍怎么在Android中将字符串数据存储到txt文件中的基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Android是什么

Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。

代码:

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

以上就是小编为大家带来的怎么在Android中将字符串数据存储到txt文件中的全部内容了,希望大家多多支持亿速云!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI