We help IT Professionals succeed at work.

Proper way to read file line by line?

DJ_AM_Juicebox
on
Hi,

In java, what's the proper method to read a file, line by line? In my case, the data is human readable characters. It seems there are many different ways on the net. I guess I just want one large string representing the contents of the file.

Thanks
Comment
Watch Question

CERTIFIED EXPERT

Commented:
.readline() from BufferedReader.
Bart CremersJava Architect

Commented:

BufferedReader reader = new BufferedReader(file);
 
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
   // process line
}

Open in new window

CERTIFIED EXPERT

Commented:
BufferedReader in  = new BufferedReader(new FileReader("somefile")); would be good.
Commented:
for more;
http://www.exampledepot.com/egs/java.io/ReadLinesFromFile.html
try {
        BufferedReader in = new BufferedReader(new FileReader("infilename"));
        String str;
        while ((str = in.readLine()) != null) {
            process(str);
        }
        in.close();
    } catch (IOException e) {
    }

Open in new window

CERTIFIED EXPERT

Commented:
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
CERTIFIED EXPERT

Commented:
!?

Explore More ContentExplore courses, solutions, and other research materials related to this topic.