Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

adding new lines?

 
Ty Karl
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Im reading a file in and wanting to display it, but its all getting presented on one long line unlike it is in the text file?

here is my code so far.

try {
String one = "";
String two = "";
fr = new FileReader("topp.txt");
in = new BufferedReader(fr);
while ((one = in.readLine()) != null) {
two = two + one;
}
jTextArea1.setText(two);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do the javadocs of BufferedReader.readLine say about its handling of line termination characters?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may be partly a Swing layout issue. I've just answered this question in your previous multi (cross?) post. You need to call setLineWrap(true) and setWrapStyleWord(true) on your JTextArea.
 
Ty Karl
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf,

ive read that now but not sure how to incorporate it in to my code???
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since BufferedReader removes the line-termination character(s), you'll need to add them back. You'll need to find the OS specific line separator via


Also rather than do String concatenation, consider using a StringBuilder object instead.
 
Ty Karl
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks pete, ill start tinkering with the stringbuilder
 
Rob Camick
Ranch Hand
Posts: 2703
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why did you repost? You where given the answer in your original posting. The suggestion was to read the JTextArea API for information about the read(...) method:



Also, don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.
 
Get off me! Here, read this tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!