-
reading from a file
Hello,
I'm very new to java and trying to figure out how to read a line of data from a file. Specifically, I'm given a text file with a set of integers on each line. I need to read one line of data at a time and then store all the integers from that line into an array. Question is how can I read one line of data from the file?
Thanks.
-
Re: reading from a file
Easiest I think is to use a Scanner object that has been constructed with the File as a parameter, and then read in each line with the Scanner. The Scanner Tutorial will give you some guidance, although you will be using the hasNextLine() and nextLine() methods of the Scanner if you are trying to read in lines from the file.
-
Re: reading from a file
Another suggestion is using the class BufferedReader, it bring the readLine() method, which acts as a Buffered of the file and read all the lines. Heres the tutorial: Java Read File Line By Line,How to Read File Line By Line in Java