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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • WebGame开发

    WebGame开发web策略类游戏开发(六)缓存概述 公布一个小webgame代码 webgame创意之《吞噬者》 webgame创意之《超时空要塞之边境》 webgame创意之《坐庄》 web策略类游戏开发(五)数据库表设计web策略类游戏开发(四)一个可以承载万人在线的架

    2022年5月2日
  • Java8学习笔记(一)–Lambda表达式

    Java8学习笔记(一)–Lambda表达式两个概念函数式接口函数式接口就是只显式声明一个抽象方法的接口。为保证方法数量不多不少,java8提供了一个专用注解,这样,当接口中声明的抽象方法多于或少于一个时就会报错。如下图所示:L

    2022年8月3日
  • drawcall优化_unity降低drawcall

    drawcall优化_unity降低drawcallunity3D对于移动平台的支持无可厚非,但是也有时候用Unity3D开发出来的应用、游戏在移动终端上的运行有着明显的效率问题,比如卡、画质等各种问题。自己在做游戏开发的时候偶有所得。对于主要影响性能的因素做个总结。主要因素有:       1.     Savedbybatching值过大  —->这个值主要是针对Mesh的批处理,这个值越高,应用就越卡

    2022年9月2日
  • 微信公众平台开发(十) 消息回复总结

    微信公众平台开发(十) 消息回复总结一、简介微信公众平台提供了三种消息回复的格式,即文本回复、音乐回复和图文回复,在这一篇文章中,我们将对这三种消息回复的格式做一下简单讲解,然后封装成函数,以供读者使用。二、思路分析对于每一个POST请

    2022年8月5日
  • 二级菜单导航栏(左侧)「建议收藏」

    二级菜单导航栏(左侧)「建议收藏」目标:实现左侧的二级导航栏效果图:项目演示:点击跳转到演示地址代码:<!DOCTYPEhtml><html> <head> <metacharset=”utf-8″> <title>左侧导航栏</title> </head> <style> body{ margin:0; padding:0; min-width:1200px; min-he

    2022年5月27日
  • ExtJs自学教程(1):一切从API開始

    ExtJs自学教程(1):一切从API開始

    2021年12月10日

发表回复

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

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