开发者

Reading from file with DataInputStream is very slow

I have got a file containing a large amount of numbers.

I have tried to use the following code to read it from the file, but it is super slow anyone can help to reduce the time?

Following is my code to read it in a very slow way:

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import开发者_开发知识库 java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

public class FileInput {

  public static void main(String[] args) {

    Scanner scan1 = new Scanner(System.in);
    String filename = scan1.nextLine();

    File file = new File(filename);
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;

    try {
          fis = new FileInputStream(file);

      bis = new BufferedInputStream(fis);
      dis = new DataInputStream(bis);

      while (dis.available() != 0) {

        System.out.println(dis.readLine());
      }

      fis.close();
      bis.close();
      dis.close();

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}


Don't use a DataInputStream to read lines from a file. Instead, use a BufferedReader, as in:

fis = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
while ((String line = reader.readLine()) != null) {
  System.out.println(line);
}

The javadoc on DataInputStream.readLine tells you to not use that method. (it's been deprecated)

Of course, when you actually get around to reading the numbers, I'd encourage you to forget reading the lines yourself, and just let Scanner read the numbers for you. If you need to know which numbers were on the same line, Scanner can do that for you too:

Scanner fileScanner = new Scanner(file, "UTF-8").useDelimiter(" +| *(?=\\n)|(?<=\\n) *");
while (fileScanner.hasNext()) {
  List<Integer> numbersOnLine = new ArrayList<Integer>();
  while (fileScanner.hasNextInt()) {
    numbersOnLine.add(fileScanner.nextInt());
  }
  processLineOfNumbers(numbersOnLine);
  if (fileScanner.hasNext()) {
    fileScanner.next(); // clear the newline
  }
}

That fancy regex makes it so that the newline characters between lines also show up as tokens to the Scanner.


It runs much faster on my machine with the println is commented out. Writing to the screen slows things down a lot. And that's not just a java thing...happens in C/C++ and every other language I've worked with.


import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;


public class file {
public static void main(String[] args){
    Scanner keyboard = new Scanner(System.in);
    String fname = "";
    System.out.print("File Name: ");
    fname = keyboard.next();

    try{
        Scanner file1 = new Scanner(new FileReader(fname));
        System.out.println("File Open Successful");
        int length = file1.nextInt();
        String[] content = new String[length];
        for (int i=0;i<length;i++){
            content[i] = file1.next();
        }
        for (int i=0;i<length;i++){
            System.out.println("["+i+"] "+content[i]);
        }
        System.out.println("End of file.");

    } catch (FileNotFoundException e){
        System.out.println("File Not Found!");
    }   


}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜