0

I am reading in from a file (which is a list of names and their contact numbers) and displaying it in a textarea in a GUI. The file reads in fine and the text is displayed. But I want each line from the file to be on a new line on the GUI. So each name and address to be on a new line. How do I do this? This is my code so far, but it doesn't display each line from the file on a new line on the GUI.

public void books() throws IOException { String result = " "; String line; LineNumberReader lnr = new LineNumberReader(new FileReader(newFile("books2.txt"))); while ((line = lnr.readLine()) != null) { result += line;` } area1 = new JTextArea(" label 1 "); area1.setText(result); area1.setBounds(50, 50, 900, 300); area1.setForeground(Color.black); panelMain.add(area1); } 
4
  • Why do you need to read it line by line if you're going to display whole file anyway? Commented Jul 21, 2016 at 18:31
  • @SergeyTachenov Because I want a new line for each name, and at the moment it displays in just one line on the GUI and does not fit on. Commented Jul 21, 2016 at 18:59
  • 1
    but do you realize that it displays in just one line precisely because you are reading it line by line? Commented Jul 21, 2016 at 19:04
  • @SergeyTachenov How do I change it so that it will display each new line from the file on a new line in the GUI? Commented Jul 21, 2016 at 19:17

1 Answer 1

1

You don't really need to read it line by line. Something like this will do:

String result = new String(Files.readAllBytes(Paths.get("books2.txt")), StandardCharsets.UTF_8); 

This, of course, will require more memory: first to read bytes, and then to create a string. But if memory is a concern, then reading the whole file at once is probably a bad idea anyway, not to mention displaying it in a JTextArea!

It may not handle different line endings properly. When you use readLine(), it strips the line of all endings, be it CR LF, LF or CR. The way above will read the string as-is. So maybe reading it line-by-line is not a bad idea after all. But I've just checked—JTextArea seems to handle CR LF all right. It may cause other problems, though.

With line-by-line approach, I'd do something like

String result = String.join("\n", Files.readAllLines(Paths.get("books2.txt"), StandardCharsets.UTF_8)); 

This still strips the last line of EOL. If that's important (e. g., you want to be able to put text cursor on the line after the last one), just do one more + "\n".

All of the above requires Java 7/8.

If you're using Java 6 or something, then the way you do it is probably OK, except that:

  1. Replace LineNumberReader with BufferedReader—you don't need line numbers, do you?
  2. Replace String result with StringBuilder result = new StringBuilder(), and += with result.append(line).append('\n').
  3. In the end, use result.toString() to get the string.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.