美文网首页java学习工作专题
9.2Java大文件读写操作

9.2Java大文件读写操作

作者: 孔垂云 | 来源:发表于2017-06-01 00:32 被阅读846次

在读写大文件, 比如超过100M或者1G的文件时,还用简单的fileinput和fileoutput是不行的,这样很容易导致内存溢出。在处理大文件的读写时,需要采用更好的方式来处理,通常来讲,是利用BufferReaderBufferWriter

读取文件的两种方式

  /**
     * 读取大文件
     *
     * @param filePath
     */
    public void readFile(String filePath) {
        FileInputStream inputStream = null;
        Scanner sc = null;
        try {
            inputStream = new FileInputStream(filePath);
            sc = new Scanner(inputStream, "UTF-8");
            while (sc.hasNextLine()) {
                String line = sc.nextLine();
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (sc != null) {
                sc.close();
            }
        }
    }

    /**
     * 读取文件
     *
     * @param filePath
     */
    public void readFile2(String filePath) {
        File file = new File(filePath);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file), 5 * 1024 * 1024);   //如果是读大文件,设置缓存
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                System.out.println(tempString);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

上面讲了两种读取文件的方式,第一种是利用Scanner扫描文件,第二种利用BufferedReader设置缓冲,来读取文件。

当然在读取过程中,即System.out.println这句话进行数据的处理,而不能把文件都放到内存中。

文件写入

  /**
     * 写文件
     * @param filePath
     * @param fileContent
     */
    public void writeFile(String filePath, String fileContent) {
        File file = new File(filePath);
        // if file doesnt exists, then create it
        FileWriter fw = null;
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(fileContent);
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

写文件利用BufferedWriter,可以保证内存不会溢出,而且会一直写入。

源码下载

本例子详细源码

相关文章

  • 9.2Java大文件读写操作

    在读写大文件, 比如超过100M或者1G的文件时,还用简单的fileinput和fileoutput是不行的,这样...

  • store模块阅读8:MappedFile

    说明 对于 commitlog、 consumequeue、 index 三类大文件进行磁盘读写操作,均是通过 M...

  • PHP读写大文件操作

    配置项 $fileNameRead = 'a.log';//路径 + 文件名 $fileNameWrite = '...

  • 管道

    以传统读写流方式复制大文件 以管道形式复制大文件 大文件压缩(链式流)

  • 文件操作

    打开方式 按行读取 大文件读写 ----按行 文件/目录的常用操作 os.path.isdir()判断是否位目录,...

  • nodeJS 文件读写

    参考文件 大文件读写使用缓冲区方式

  • Java当中的IO(2)

    1.大文件的读写方法2.字符流的使用方法 总结,大文件读写,创建一个数组,用一个循环,每次从文件中读取一部分放入数...

  • 大文件txt读写

  • node读写大文件

    如果读取小文件,我们可以使用fs读取,fs读取文件的时候,是将文件一次性读取到本地内存。而如果读取一个大文件,一次...

  • Node大内存应用

    在Node中,不可避免地还是会存在操作大文件的场景。由于Node的内存限制,操作大文件也需要小心,好在Node提供...

网友评论

    本文标题:9.2Java大文件读写操作

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