Java: Read text file efficiently with BufferedReader

preview_player
Показать описание
Read a text file with standard Java BufferedReader. This is faster than RandomAccessFile, but slightly more lines of code. Demo of BufferedReader and FileReader. We process the text file line by line and then close the text file. Good beginning programming example. Follow along!
Рекомендации по теме
Комментарии
Автор

your welcome. sounds like you finally got your code working. congrats! feel free to suggest other videos

cbttjm
Автор

You need to accumulate the lines into a StringBuilder and then call setText on the jtextarea. So your first 10 lines should be something like this:

BufferedReader read = new BufferedReader(new FileReader("try.txt"));
StringBuilder sb = new StringBuilder();
String line = read.readLine();
while (line != null) {
line = read.readLine();
sb.append(line).append("\n");
}
area1 = new JTextArea(10, 100);
area1.setEditable(false);
area1.setText(sb.toString());

cbttjm
Автор

// Before opening the file, make a new StringBuilder:
StringBuilder sb = new StringBuilder();
// now each time through the loop, add the line read from the file to the string builder
sb.append(line).append("\n");
// after the loop, put the string builder content in the JTextArea

cbttjm