Proper way to read file line by line?

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
DJ_AM_JuiceboxAsked:
Who is Participating?
 
ysnkyConnect With a Mentor 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

0
 
krakatoaCommented:
.readline() from BufferedReader.
0
 
Bart CremersJava ArchitectCommented:

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

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
krakatoaCommented:
BufferedReader in  = new BufferedReader(new FileReader("somefile")); would be good.
0
 
krakatoaCommented:
!?
0
All Courses

From novice to tech pro — start learning today.