Android写入txt文件并读取

作者: 你在笑吗 | 来源:发表于2017-12-19 17:31 被阅读1985次

直接上代码~

注意权限

<!--手机存储-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

1.写入数据

    private void writeData() {
        String filePath = "/sdcard/Gyt/";
        String fileName = "data.txt";
        writeTxtToFile("Wx:lcti1314", filePath, fileName);
    }

// 将字符串写入到文本文件中
    private 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);
        }
    }

//生成文件

  private 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;
    }

//生成文件夹

    private 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 + "");
        }
    }

2.读取数据

//读取指定目录下的所有TXT文件的文件内容
    private 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;
    }

3.代码调用

代码调用.png

2.效果展示

效果展示.gif

相关文章

  • 长知识系列 - 收藏集 - 掘金

    SpringBatch 读取 txt 文件并写入数据库 - 后端 - 掘金SpringBatch 读取 txt 文...

  • 长知识 - 收藏集 - 掘金

    SpringBatch 读取 txt 文件并写入数据库 - 后端 - 掘金SpringBatch 读取 txt 文...

  • Android写入txt文件并读取

    直接上代码~ 注意权限 1.写入数据 2.读取数据 3.代码调用 2.效果展示

  • Winform文件相关操作

    复制文件 获取文件夹下的所有文件名称 创建txt文件,并写入数据 读取txt文件 选择文件夹

  • nodeJS读取json文件并写入txt或redis中

    【1:读取json写入txt文件】json文件book.json js文件 【2:读取json写入redis文件/...

  • txt读写

    文件打开 读文件 读取字符串 按行读取整个文件 写文件 字符串写入txt 列表写入文件 双层列表写入文件 数组写入文件

  • 读写txt/rtf文件

    目录:1、txt文件写入2、txt文件读取3、rtf文件读取4、NSString转换成其他类型数据方法 1、txt...

  • 文件流

    写入流 writeStream.txt 读取流 读取流与写入流结合 文件流管道

  • 利用Python处理Excel数据

    读取数据 读取x.xlsx文件 读取文件夹 读取txt文件 读取csv格式Excel表 写入excel 显示数据 ...

  • 1.python文件操作

    操作格式w:写入r:读取b:二进制a+:追加操作 打开文件 显示已经读取的文件内容 关闭文件 创建一个txt文件并...

网友评论

    本文标题:Android写入txt文件并读取

    本文链接:https://www.haomeiwen.com/subject/qydiwxtx.html