2-SII--应用本包下文件写入和读取

零、先说一下我的IO小工具方法:

1.IO读写:
9414344-2695d801081f28fb.png
IO.png
2.读取InputStream
    /**
     * 读取InputStream
     *
     * @param is 输入流
     * @return 流转化的字符串
     * @throws IOException IO异常
     */
    private static String read(InputStream is) throws IOException {
        byte[] temp = new byte[1024];
        int len = 0;
        StringBuilder sb = new StringBuilder("");
        while ((len = is.read(temp)) > 0) {
            sb.append(new String(temp, 0, len));
        }
        return sb.toString();
    }
3.写入OutputStream
    /**
     * 写入OutputStream
     *
     * @param os 输出流
     * @param fileContent 文本内容
     */
    private void write(OutputStream os, String fileContent) {

        try {
            if (fileContent != null) {
                os.write(fileContent.getBytes());
            }
            close(os);//关闭输出流
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
4.关闭可关闭对象
    /**
     * 关闭可关闭对象
     *
     * @param io 可关闭对象
     */
    private void close(Closeable io) {
        if (io != null) {
            try {
                io.close();
            } catch (IOException e) {
                L.e(e);
            }
        }
    }

一、本包下文件读写:

写入/data/data/com.toly1994.sii_file/files/writeInLocal.txt
//写入数据 追加模式在/data/data/com.toly1994.sii_file/files文件家下创建writeInLocal.txt,内容"toly"
fileHelper.writeInLocal(this, "writeInLocal.txt", "toly", true);
//写入数据 覆盖模式在/data/data/com.toly1994.sii_file/files文件家下创建writeInLocal_2.txt,内容"toly"
fileHelper.writeInLocal(this, "writeInLocal_2.txt", "toly", false);
9414344-8e16e46549d2ed5a.png
writeInLocal.png
读取:/data/data/com.toly1994.sii_file/files/writeInLocal.txt
String data = fileHelper.readInLocal(this, "writeInLocal.txt");
Log.e(TAG, data);//tolytolytoly

数据写入:
    /**
     * 在data/data/本包中写入文件:追加文件模式
     *
     * @param fileName     文件名
     * @param fileContent 文件内容
     * @param append 是否以追加模式
     */
    public void writeInLocal(Context ctx,String fileName, String fileContent,boolean append) {
        FileOutputStream fos = null;
        try {
            fos = ctx.openFileOutput(fileName, append?Context.MODE_APPEND:Context.MODE_PRIVATE);
            write(fos, fileContent);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            close(fos);
        }
    }
读取数据:
    /**
     * 在data/data/本包中读取文件
     *
     * @param fileName 文件名
     * @return 文件内容
     */
    public String readInLocal(Context ctx,String fileName) {
        FileInputStream fis = null;
        String result = null;
        try {
            fis = ctx.openFileInput(fileName);
            result = read(fis);
            //关闭输入流
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(fis);
        }
        return result;
    }

二、读取assets文件夹的文件

9414344-0c0edaa4bb3d371b.png
assets.png
代码调用
String assetsData = fileHelper.readInAssets(this, "act.json");
Log.e(TAG, assetsData);//{"name":"toly"}
代码实现
    /**
     * //////////////////////////从assets 中读取文件/////////////
     *
     * @param fileName 文件名
     * @return 文件内容
     */
    public String readInAssets(Context ctx,String fileName) {
        InputStream is = null;
        String result = null;
        try {
            is = ctx.getAssets().open(fileName);
            result = read(is);
            //关闭输入流
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(is);
        }
        return result;
    }


后记、

1.声明:

[1]本文由张风捷特烈原创,转载请注明
[2]欢迎广大编程爱好者共同交流
[3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正
[4]你的喜欢与支持将是我最大的动力

2.连接传送门:

更多安卓技术欢迎访问:安卓技术栈
我的github地址:欢迎star
简书首发,腾讯云+社区同步更新
张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com

3.联系我

QQ:1981462002
邮箱:1981462002@qq.com
微信:zdl1994328

4.欢迎关注我的微信公众号,最新精彩文章,及时送达:
9414344-c474349cd3bd4b82.jpg
公众号.jpg
原文地址:https://www.cnblogs.com/toly-top/p/9781939.html