简体   繁体   中英

How to read large text files faster in Java?

I just made a simple sequential median filter algorithm and I happened to have used 2 scanners to handle command-line input and a scanner for reading the file. Now the problem is,I have a text file of 2,000,000 lines of 2 columns text in the form of <integer> <float> and it's taking a long time(more than 2 minutes) just to read the file.

Basically the program simply grabs the input, uses the median filter algorithm and writes to an output file.

Below is my code: 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.
    }   
}

Is there a way to read the file faster(as in seconds)?

Thanks

Edit: Example input

5
1 2
2 80
3 6
4 3
5 1

Using Buffered Reader should improve the speed it takes to read the file as it has a much larger buffer size than Scanner.

I see from one of your previous questions that you were using Buffered Reader initially. You can read in line by line and split the string on the space, like so:

    //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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM