Reputation: 161
I'm still in the process of learning so please correct me if I'm misunderstanding, but shouldn't the FileReader object return the entire contents of a text file?
I have a snippet of code here where I'm simple trying to take the contents of a short .txt file, and print it using system.out.println()
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
File testDoc = new File("C:\\Users\\Te\\Documents\\TestDocument.txt");
BufferedReader reader = new BufferedReader(new FileReader(testDoc));
Scanner in = new Scanner(new FileReader(testDoc));
try {
System.out.println(reader.readLine());
} finally {
reader.close();
}
}
}
The .txt file contains only 3 lines, formatted like so:
some text here, more text and stuff
new estonian lessons
word = new word
However the program is only printing the first line in the file.
some text here, more text and stuff
What is causing this, and how do I correct it?
I've tried reading the documentation, as well as searching through Stackoverflow, but I haven't been able to find the solution to this problem.
Upvotes: 1
Views: 5809
Reputation: 131326
You may use the Scanner that is actually instantiated but not used to chain it with a FileReader
instance.
It could allow to have the flexible api of Scanner
class that has hasNextLine()
and nextLine()
methods.
Scanner in = new Scanner(new FileReader(testDoc));
public static void main(String[] args) throws FileNotFoundException, IOException {
File testDoc = new File("C:\\TestDocument.txt");
Scanner in = new Scanner(new FileReader(testDoc));
try {
while (in.hasNextLine()) {
String currentLine = in.nextLine();
System.out.println(currentLine);
}
} finally {
in.close();
}
}
Upvotes: 1
Reputation: 891
Two ways to do it. See below.
File testDoc = new File("C:\\Users\\Te\\Documents\\TestDocument.txt");
BufferedReader reader = new BufferedReader(new FileReader(testDoc));
Scanner in = new Scanner(new FileReader(testDoc));
try {
//Using Scanner Object
while(in.hasNextLine()){
System.out.println(in.nextLine());
}
//Using BufferReader Object
String line=reader.readLine();
while(line!=null){
System.out.println(line);
line=reader.readLine();
}
} finally {
reader.close();
}
Upvotes: 0
Reputation: 22384
BufferedReader
's (look here) readLine()
reads a single line from the file, so you need to write a loop as shown below:
String line = "";
while((line =reader.readLine()) != null) {
System.out.println(line);
}
Also, you don't need Scanner
object in your code (if you use BufferedReader
), so it would be simply as shown below:
File testDoc = new File("C:\\Users\\Te\\Documents\\TestDocument.txt");
BufferedReader reader = new BufferedReader(new FileReader(testDoc));
try {
String line = "";
while((line =reader.readLine()) != null) {
System.out.println(line);
}
} finally {
reader.close();
}
Upvotes: 1
Reputation: 1171
The method readLine()
just returns one line. So you have to iterate over all lines:
while(reader.hasNextLine()){
String currentLine = reader.readLine();
}
Upvotes: 0