9.2Java大文件读写操作

在读写大文件, 比如超过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,可以保证内存不会溢出,而且会一直写入。

源码下载

本例子详细源码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 因为自己的Java基本功不扎实,所以这篇文章主要介绍Java中的流操作,让自己再学习一下。 Java IO Jav...
    卡咔喀阅读 13,441评论 0 5
  • # 3.1 File # ## 3.1.1 File基本概念 ## 1.基本概念 -File类用于表示文件(目录)...
    闫子扬阅读 498评论 0 0
  • JAVA面试题 1、作用域public,private,protected,以及不写时的区别答:区别如下:作用域 ...
    JA尐白阅读 1,191评论 1 0
  • 日出东际落西山,四季更迁岁复年。 蹈矩循规星月日,乱章造次事无安。 胆于越界招灾难,时刻危机引患参。 万事矩规时处...
    徐一村阅读 324评论 0 4
  • 不擅长写文章,只是随笔。 在一起三年了,磕磕碰碰、吵吵闹闹,哭过、笑过、吵过、闹过、醉过,有过不安,有...
    彪悍的二哈阅读 192评论 0 1