-3

I need to read from a file that contain 9000 words, what is the best way to read from this file and what is the difference between bufferingreader aND regular scanner.. or is there other good class to use? Thanks

Ori Dahan
  • 1
  • 1
  • You might want to read Java NIO [documentation](https://docs.oracle.com/javase/7/docs/api/java/nio/package-summary.html). – VHS Dec 05 '16 at 15:19
  • Have you tried to read the documentation of BufferedReader and Scanner? – theVoid Dec 05 '16 at 15:19

2 Answers2

0

If you are doing "efficient" reading, there is no benefit to buffering. If, on the other hand, you are doing "inefficient" reading, then having a buffer will improve performance.

What do I mean by "efficient" reading? Efficient reading means reading bytes off of the InputStream / Reader as fast as they appear. Imagine you wanted to load a whole text file to display in an IDE or other editor. "inefficient" reading is when you are reading information off of the stream piecemeal - ie Scanner.nextDouble() is inefficient reading, as it reads in a few bytes (until the double's digits end), then transforms the number from text to binary. In this case, having a buffer improves performance, as the next call to nextDouble() will read out of the buffer (memory) instead of disk

If you have any questions on this, please ask

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
0

Open the file using an input stream. Then read its content to a string using this code:

public static void main(String args[]) throws IOException {

    FileInputStream in = null;

    in = new FileInputStream("input.txt");

    String text = inputStreamToString(is);

}

// Reads an InputStream and converts it to a String.
public String inputStreamToString(InputStream stream) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    while((length = stream.read(buffer)) != -1)
        byteArrayOutputStream.write(buffer,0,length);
    return byteArrayOutputStream.toString("UTF-8");
}

Check this answer for comparisons between buffered readers:

Read/convert an InputStream to a String

I normally use Scanners when I want to read a file line by line, or based on a delimiter. For example:

try {
    Scanner fileScanner = new Scanner(System.in);

    File file = new File("file.txt");

    fileScanner = new Scanner(file);


    while (fileScanner.hasNextLine()) {
        String line = fileScanner.nextLine();
        System.out.println(line);
    }
    fileScanner.close();
} catch (Exception ex) {
    ex.printStackTrace();
}

To scan based on a delimiter, you can use something similar to this:

fileScanner.userDelimiter("\\s"); // \s matches any whitespace
while(fileScanner.hasNext()){
    //do something with scanned data
    String word = fileScanner.next();
    //Double num = fileScanner.nextDouble();
}
Community
  • 1
  • 1
yogur
  • 810
  • 10
  • 19
  • The [`ByteArrayOutputStream.toString()`](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#toString--) uses the platform dependent character encoding. You should never rely on the platform dependent encoding (which is Cp1252 for Windows and usually UTF-8 for Linux). Use always an explicit encoding instead to avoid hard to find bugs. – vanje Dec 05 '16 at 16:20