Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a plain text file in Java

It seems there are different ways to read and write data of files in Java.

I want to read ASCII data from a file. What are the possible ways and their differences?

like image 706
Tim the Enchanter Avatar asked Jan 17 '11 18:01

Tim the Enchanter


People also ask

How do you read write a text file in Java?

There are several ways to read a plain text file in Java e.g. you can use FileReader, BufferedReader, or Scanner to read a text file. Every utility provides something special e.g. BufferedReader provides buffering of data for fast reading, and Scanner provides parsing ability.

How do you read the contents of a file in Java?

We can use Files class to read all the contents of a file into a byte array. Files class also has a method to read all lines to a list of string. Files class is introduced in Java 7 and it's good if you want to load all the file contents.

How do you load a file in Java?

Example 1: Java Program to Load a Text File as InputStream txt. Here, we used the FileInputStream class to load the input. txt file as input stream. We then used the read() method to read all the data from the file.

How do I read a file?

First, open a text file for reading by using the open() function. Second, read text from the text file using the file read() , readline() , or readlines() method of the file object. Third, close the file using the file close() method.


2 Answers

ASCII is a TEXT file so you would use Readers for reading. Java also supports reading from a binary file using InputStreams. If the files being read are huge then you would want to use a BufferedReader on top of a FileReader to improve read performance.

Go through this article on how to use a Reader

I'd also recommend you download and read this wonderful (yet free) book called Thinking In Java

In Java 7:

new String(Files.readAllBytes(...)) 

(docs) or

Files.readAllLines(...) 

(docs)

In Java 8:

Files.lines(..).forEach(...) 

(docs)

like image 34
Aravind Yarram Avatar answered Sep 27 '22 20:09

Aravind Yarram


My favorite way to read a small file is to use a BufferedReader and a StringBuilder. It is very simple and to the point (though not particularly effective, but good enough for most cases):

BufferedReader br = new BufferedReader(new FileReader("file.txt")); try {     StringBuilder sb = new StringBuilder();     String line = br.readLine();      while (line != null) {         sb.append(line);         sb.append(System.lineSeparator());         line = br.readLine();     }     String everything = sb.toString(); } finally {     br.close(); } 

Some has pointed out that after Java 7 you should use try-with-resources (i.e. auto close) features:

try(BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {     StringBuilder sb = new StringBuilder();     String line = br.readLine();      while (line != null) {         sb.append(line);         sb.append(System.lineSeparator());         line = br.readLine();     }     String everything = sb.toString(); } 

When I read strings like this, I usually want to do some string handling per line anyways, so then I go for this implementation.

Though if I want to actually just read a file into a String, I always use Apache Commons IO with the class IOUtils.toString() method. You can have a look at the source here:

http://www.docjar.com/html/api/org/apache/commons/io/IOUtils.java.html

FileInputStream inputStream = new FileInputStream("foo.txt"); try {     String everything = IOUtils.toString(inputStream); } finally {     inputStream.close(); } 

And even simpler with Java 7:

try(FileInputStream inputStream = new FileInputStream("foo.txt")) {          String everything = IOUtils.toString(inputStream);     // do something with everything string } 
like image 164
Knubo Avatar answered Sep 27 '22 21:09

Knubo