read data from text file and display results line by line. I\'ve coded this, but
ID: 3557714 • Letter: R
Question
read data from text file and display results line by line. I've coded this, but it only displays the first line.
import java.nio.file.*;
import java.io.*;
public class DisplaySavedEmployeeList {
public static void main(String[] args) {
Path file = Paths.get("C:\Users\Jeremiah\Documents\Test\writeFile.txt");
InputStream input = null;
try {
input = Files.newInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String s = null;
s = reader.readLine();
System.out.println(s);
input.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
Explanation / Answer
import java.nio.file.*;
import java.io.*;
public class DisplaySavedEmployeeList {
public static void main(String[] args) {
Path file = Paths.get("C:\Users\Jeremiah\Documents\Test\writeFile.txt");
InputStream input = null;
try {
input = Files.newInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String s = null;
while ((s = reader.readLine()) != null) { // here is bug..u have to keep reading till last line.
System.out.println(s);
}
input.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.