如何在Java中更快地阅读大文本文件?

时间:2015-08-09 18:36:05

标签: java performance arraylist java.util.scanner bufferedreader

我刚刚制作了一个简单的顺序中值滤波算法,我碰巧使用了2个扫描仪来处理命令行输入和扫描仪来读取文件。问题是,我有一个2,000,000行2列文本的文本文件,格式为<integer> <float>,而且需要花费很长时间(超过2分钟)来阅读文件。

基本上程序只是抓取输入,使用中值滤波算法并写入输出文件。

以下是我的代码: 的 main.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class main {
    public static void main(String args[]) throws NumberFormatException, IOException{
        String inFile; //Input file name.
        int filterSize; //Filter size (odd integer >= 3).
        String outFile; //Output file name.
        int arraySize;
        List<Float> elements = new ArrayList<Float>();
        int index = 0;

        //Scanner to take input file name, filter size and output file name.
        Scanner keyboardInput = new Scanner(System.in);
        System.out.println("Enter your keyboard input as follows: <data file name> <filter size(odd int >= 3> <output file name>");

        //Assigning values to variables.
        inFile = keyboardInput.next();
        filterSize = keyboardInput.nextInt();
        outFile = keyboardInput.next();


//      //Reading file
//      Scanner readFile = new Scanner(new File(inFile));
//      readFile.nextInt(); //Get Array Size        
//      
//      //Add elements into ArrayList
//      while(readFile.hasNext()){
//          readFile.nextInt();
//          elements.add(Float.parseFloat(readFile.next()));
//      }    

        //Reading file
        BufferedReader br = new BufferedReader(new FileReader(inFile));
        br.readLine(); //Get Array Size 

        String line;
        while((line = br.readLine())!= null){
            String[] nums = line.split(" ");
            int val = Integer.valueOf(nums[0]);
            elements.add(Float.valueOf(nums[1]));
        }
        br.close();

        new Serial(elements, filterSize, outFile);

    }
}

Serial.java

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Serial {
    int filterSize; //Filter size (odd integer >= 3).
    String outFile; //Output file name.
    int arraySize;
    List<Float> elements = new ArrayList<Float>();
    int index = 0;


    public Serial(List<Float> elements, int filterSize, String outFile) throws FileNotFoundException, UnsupportedEncodingException {
        this.elements = elements;
        this.filterSize= filterSize;
        this.outFile = outFile;


        List<Float> tempElements = new ArrayList<Float>();
        List<Float> outputElements = new ArrayList<Float>();

        //Add first boundary element to ouput ArrayList
        outputElements.add(this.elements.get(0));

        while(elements.size() >= filterSize){
            for(int i = 0; i<filterSize; i++){
                tempElements.add(this.elements.get(i));
            }

            Collections.sort(tempElements);
            outputElements.add(tempElements.get((filterSize-1)/2));

            elements.remove(0);
            tempElements.clear();
        }

        //Add last boundary element to output ArrayList
        if (elements != null && !elements.isEmpty()) {
            outputElements.add(elements.get(elements.size()-1));
        }

        /*Trace. Checking if output is correct
        for(int i=0; i<outputElements.size(); i++){
            System.out.println(outputElements.get(i));
        }*/

        //Write elements to output file     
        PrintWriter writeOutput = new PrintWriter(this.outFile, "UTF-8");
        writeOutput.println(outputElements.size());//Number of lines
        for(int i=0; i<outputElements.size();i++){
            writeOutput.println(i+1 + " " + outputElements.get(i)); //Each line is written
        }

        writeOutput.close(); //Close when output finished writing.
    }   
}

有没有办法更快地读取文件(以秒为单位)?

由于

编辑: 输入示例

5
1 2
2 80
3 6
4 3
5 1

1 个答案:

答案 0 :(得分:3)

使用Buffered Reader可以提高读取文件所需的速度,因为它的缓冲区大小比扫描程序大得多。

我从您之前的一个问题中看到您最初使用的是Buffered Reader。您可以逐行阅读并在空格上分割字符串,如下所示:

    //Reading file
    BufferedReader br = new BufferedReader(new FileReader(inFile));
    br.readLine(); //Get Array Size 

    String line;
    while((line = br.readLine())!= null){
        String[] nums = line.split(" ");
        int val = Integer.valueOf(nums[0]);
        elements.add(Float.valueOf(nums[1]));
    }
    br.close();