java快速读取文件

       java读取文件的方式有多种,有些慢,有些快,当读取大量的文件时,速度尤为重要。以下是鄙人做项目时发现读取文件过慢时,对两种读取文件方式快慢的测试。

开门见山:这是快速读取的代码:

static public String readFile(File file) {      

    try {
            FileInputStream fileInputStream = new FileInputStream(file);
            // 把每次读取的内容写入到内存中,然后从内存中获取
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            // 只要没读完,不断的读取
            while ((len = fileInputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
            // 得到内存中写入的所有数据
            byte[] data = outputStream.toByteArray();
            fileInputStream.close();
            return new String(data);
            //return new String(data, "GBK");//以GBK(什么编码格式)方式转
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

这是比较慢读取的代码:

static public String getContent(File file) {
        StringBuffer result = new StringBuffer();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    new FileInputStream(file), Charset.forName("GBK")));// 构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行
                result.append(s).append("\n");
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }


这是不可取的读取的代码:

static public String getContent(File file) {

//在着再次强调一个“+”号,数据多的时间绝对不能用“+”

        String result = "";
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    new FileInputStream(file), Charset.forName("GBK")));// 构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行
                result = result + "\n" + s;
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }


测试结果:


测试类:


package com.atgeretg.test;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class Test {
    public static void main(String[] args) {
        findFile("J:\\lhc");
    }
    
    static public void findFile(String path) {
        // Dao dao = new Dao();
        File folder = new File(path);// 获取文件夹路径
        if (folder.isDirectory()) {
            String[] nameS = folder.list();// 获取文件名
            String pathWindows = folder.getAbsolutePath();
            System.out.println("path = " + pathWindows);
            String[] cont = new String[nameS.length];
            long millisRead = System.currentTimeMillis();
            for (int i = 0; i < nameS.length; i++) {
                String filePath = pathWindows + "\\" + nameS[i];
                //System.out.println(filePath);
                File f = new File(filePath);
                String content = readFile(f);
                // getNeedData(content);
                cont[i] = content;
            }

            System.out.println("read 快的 time = "
                    + (System.currentTimeMillis() - millisRead)+"ms; 共:"+nameS.length+"个文件");
            millisRead = System.currentTimeMillis();
            for (int i = 0; i < nameS.length; i++) {
                String filePath = pathWindows + "\\" + nameS[i];
                //System.out.println(filePath);
                File f = new File(filePath);
                String content = getContent1(f);
                // getNeedData(content);
                cont[i] = content;
            }

            System.out.println("read 没有加号的 time = "
                    + (System.currentTimeMillis() - millisRead)+"ms; 共:"+nameS.length+"个文件");
            millisRead = System.currentTimeMillis();
            for (int i = 0; i < nameS.length; i++) {
                String filePath = pathWindows + "\\" + nameS[i];
                //System.out.println(filePath);
                File f = new File(filePath);
                String content = getContent(f);
                // getNeedData(content);
                cont[i] = content;
            }
            System.out.println("read 带有加号的 time = "
                    + (System.currentTimeMillis() - millisRead)+"ms; 共:"+nameS.length+"个文件");
        }
    }

    static public String getContent(File file) {
        String result = "";
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    new FileInputStream(file), Charset.forName("GBK")));// 构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行
                result = result + "\n" + s;
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
    static public String getContent1(File file) {
        StringBuffer result = new StringBuffer();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    new FileInputStream(file), Charset.forName("GBK")));// 构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行
                result.append(s).append("\n");
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // result.getBytes(charset)
        return result.toString();
    }

    static public String readFile(File file) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            // 把每次读取的内容写入到内存中,然后从内存中获取
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            // 只要没读完,不断的读取
            while ((len = fileInputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
            // 得到内存中写入的所有数据
            byte[] data = outputStream.toByteArray();
            fileInputStream.close();
            //return new String(data);
            return new String(data, "GBK");//以GBK方式转,不会出现中文乱码就不在加,加上速度会相对慢一些
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    
    static public String readFile1(File file) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            // 把每次读取的内容写入到内存中,然后从内存中获取
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            // 只要没读完,不断的读取
            while ((len = fileInputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
            // 得到内存中写入的所有数据
            byte[] data = outputStream.toByteArray();
            fileInputStream.close();
            return new String(data);
//            return new String(data, "GBK");//以GBK方式转,不会出现中文乱码就不在加,加上速度会相对慢一些
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}


  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值