Android【File文件存储工具类】

Android【File文件存储工具类】

最近写项目用到了文件存储,当需要频繁的存储操作的时候,文件存储性能方面远大于SharedPreferences 。

工具类复制可用

public class FileUtils {
   

    // 将字符串写入到文本文件中
    public static void writeTxtToFile(String strcontent, String filePath, String fileName) {
   
        //生成文件夹之后,再生成文件,不然会出错
        makeFilePath(filePath, fileName);
        String strFilePath = filePath + fileName;
        // 每次写入时,都换行写
        String strContent = strcontent + "\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) {
   
            ToastUtils.show("请检查存储权限");
        }
    }

    //生成文件
    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 + "";
                        }
                        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;
    }

    public static void DeleteFile(String path) {
   
        File file = new File(path);
        if (file.exists()) {
   
            file.deleteOnExit();
        }
    }

    public static void updateText(String strcontent, String filePath, String fileName) {
   
        makeFilePath(filePath, fileName);
        String strFilePath = filePath + fileName;
        String strContent = strcontent;
        try {
   
            File file = new File(strFilePath);
            if (!file.exists()) {
   
                file.getParentFile().mkdirs();
                file.createNewFile();
            }
            //写入的txt文档的路径
            PrintWriter pw = new PrintWriter(strFilePath);
            //写入的内容
            pw.write(strContent);
            pw.flush();
            pw.close();
        } catch (Exception e) {
   
            ToastUtils.show("请检查存储权限");
        }
    }

    /** * 读取changedata请求的id * @return 请求的id */
    public static String getXctId() {
   
        String id = getFileContent(new File("/sdcard/xct/id.txt"));
        return TextUtils.isEmpty(id) ? "0" : id;
    }

    /** * 保存changedata的id * @param id */
    public static void saveId(String id) {
   
        updateText(id, "/sdcard/xct/", "id.txt");
    }

    /** * 保存广告 * @param advertStr */
    public static void saveAdvert(String advertStr){
   
        updateText(advertStr, "/sdcard/xct/", "advert.txt");
    }

    /** * 读取广告数据 * @return */
    public static String getAdvert() {
   
        String advert = getFileContent(new File("/sdcard/xct/advert.txt"));
        return advert==null? "":advert;
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/2813.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 国外最流行的Bootstrap后台管理模板

    国外最流行的Bootstrap后台管理模板工欲善其事,必先利其器对于从事软件开发的您也一样,有一套熟悉的bootstrap后台ui框架让您的开发速度大幅度提升这是本人经常使用到的一些bootstrap后台框架推荐给大家第一名inspiniabootstrap演示地址http://cn.inspinia.cn效果图http://cn.inspinia.cnhttp://cn.inspinia.cn第二名…

    2022年4月25日
    47
  • android.app.Fragment$InstantiationException的原因分析

    android.app.Fragment$InstantiationException的原因分析每个Fragment必须要有一个无参构造方法,这样该Fragment在Activity恢复状态的时候才可以被实例化。强烈建议,Fragment的子类不要有其他含参构造方法,因为这些构造方法在Fragment重新实例化时不会被调用。取而代之的方式是,通过setArguments(Bundle)设置参数,然后通过getArguments获得参数。

    2022年7月16日
    18
  • redis可视化客户端工具TreeNMS

    redis可视化客户端工具TreeNMS

    2021年11月3日
    60
  • 数据滤波算法集合「建议收藏」

    数据滤波算法集合「建议收藏」由于要进行数据处理,就利用网络资源总结各种滤波方法以便日后查阅。一、限幅滤波法实现步骤:根据经验法选择最大偏差值E。|value_now-value_before|<=E,value_now有效,否则其无效且将其舍弃,最后令value_now=value_before。实现程序:#defineE10//value取值范围为90~110intv

    2022年5月3日
    107
  • 字节、字、bit、byte的关系「建议收藏」

    字节、字、bit、byte的关系「建议收藏」字word 字节byte 位bit 字长是指字的长度1字节=8位(1byte=8bit)1字=2字节(1word=2byte)一个字节的字长是8一

    2022年8月2日
    9
  • webstorm激活码最新2021(注册激活)

    (webstorm激活码最新2021)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~MLZPB5EL5Q-eyJsaWNlb…

    2022年3月21日
    229

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号