aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to read an entire text file and display in a textbox or text area Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to read an entire text file and display in a textbox or text area" Watch "How to read an entire text file and display in a textbox or text area" New topic
Author

How to read an entire text file and display in a textbox or text area

Ty Karl
Greenhorn

Joined: Jun 11, 2009
Posts: 22
Im a very new programmer (to java at least) and am finding alot of things confusing,
for instance this current prob at the moment, any help would be greatly appreciated

Ty
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Tyrone Toplis wrote:Im a very new programmer (to java at least) and am finding alot of things confusing,
for instance this current prob at the moment, any help would be greatly appreciated

Ty


Think about the problem in two separate parts.

1. Can you write a string to a text area?
2. Can you open a file and read it into a string, line by line?
3. If you can do 1 & 2, then you can do what you are asking.

so let me ask, where does the problem lie?
Ty Karl
Greenhorn

Joined: Jun 11, 2009
Posts: 22
thanks fred,

yes I can writr to, but if i read an entire paragraph say, would the entire paragraph fit into a single string?
or as i have done before when reading multiple records, split them?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
You can use a StringBuilder to build up a string a line at a time. Yes, you can hold a lot of text in a single string, that should not be a problem.

The following page gives an example of line by line input/output with text files.

I suggest experimenting with the code, instead of writing each line to a file as you go, append each line to a StringBuilder.

http://java.sun.com/docs/books/tutorial/essential/io/charstreams.html

p.s. you may run into issues with preserving blank lines, paragraph breaks etc as you build up a String using StringBuilder, but you/we can deal with that if/when it arises.
Ty Karl
Greenhorn

Joined: Jun 11, 2009
Posts: 22
thanks again Fred,

I will be appending my project tomorrow at college.
Dont think this is the place to do introductions etc but I will committ a sin.
Im from Cape Town, South Africa, alot of companies here are inclined to use .net etc and java is very limited therefore extremely expensive and qualified instructors hard to find,
so to find this site is a god send. Have you been in the industry long? and where?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Tyrone Toplis wrote:thanks again Fred,

I will be appending my project tomorrow at college.
Dont think this is the place to do introductions etc but I will committ a sin.
Im from Cape Town, South Africa, alot of companies here are inclined to use .net etc and java is very limited therefore extremely expensive and qualified instructors hard to find,
so to find this site is a god send. Have you been in the industry long? and where?


I just program as a hobby. I've known java for a couple of years. I live in Canada

Coderanch is a good site, there's plenty of people here with more java knowledge than I have. If you visit this site often, I can give you the following advice now. Very often you will get more than one person answering your questions, and people have different ways of responding. Some will provide detailed examples, some will point you to some documentation to do your own research. So sometimes it pays to be patient, and wait and see how a thread pans out before deciding upon a course of action.
Ty Karl
Greenhorn

Joined: Jun 11, 2009
Posts: 22
well i feel rather inadequate considering its your hobby!
anyway thank you and maybe bump into you again
cheers
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
You're welcome. Let us know what your solution ends up being.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> "How to read an entire text file and display in a textbox or text area"

JTextField and JTextArea both inherit the read() method of JTextComponent(),
which does exactly your question
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Michael Dunn wrote:> "How to read an entire text file and display in a textbox or text area"

JTextField and JTextArea both inherit the read() method of JTextComponent(),
which does exactly your question


Nice! Didn't know that one, but it looks like a much better answer than mine. Learn something every day.
Ty Karl
Greenhorn

Joined: Jun 11, 2009
Posts: 22
Hi guys,

thanks for the help, ive tried this and it works so-so, problem is that the text is all displayed on a single line in the text area, not like in the file ie paragraphed etc.

here is my code:

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();
}
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Tyrone Toplis wrote:thanks for the help, ive tried this and it works so-so, problem is that the text is all displayed on a single line in the text area, not like in the file ie paragraphed etc.


Please check out the JTextArea API where you will find two important methods, setLineWrap(boolean wrap) and setWrapStyleWord(boolean word). Call these methods on your JTextArea with both parameters set to true, and let us know if this helps. The API can be found here: JTextArea API
Ty Karl
Greenhorn

Joined: Jun 11, 2009
Posts: 22
hi peter,

thanks very much, it nearly works, just that there are no paragraphs in the text area like the file.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Tyrone Toplis wrote:Hi guys,

thanks for the help, ive tried this and it works so-so, problem is that the text is all displayed on a single line in the text area, not like in the file ie paragraphed etc.

here is my code:

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();
}


If you want to preserve lines and use what you have already done, you can always test if two == "" (blank line) and act accordingly. You can also put a line break at the end of each string you append to one.


while ((one = in.readLine()) != null) {
two = two + one +"\n";
}

Some people might say you shouldn't put a line break into a string like that, it may not work properly for all operating systems, but the fact is it will very likely work fine in this case.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
answered differently in cross-post:

adding-new-lines
Ty Karl
Greenhorn

Joined: Jun 11, 2009
Posts: 22
Hi Fred,

thanks, on the other reply, Pete mentioned the stringbuilder as you did yesterday, so im gonna try yours first then do the stringbuilder, which ive never used before,

thanks for the help
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Fred Hamilton wrote:Some people might say you shouldn't put a line break into a string like that, it may not work properly for all operating systems, but the fact is it will very likely work fine in this case.

How do you know this for a fact?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
pete stein wrote:answered differently in cross-post:

adding-new-lines


Ah I didn't know about the cross post. There's usually a few different ways of getting it done, presenting all of them can get confusing I guess.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
pete stein wrote:
Fred Hamilton wrote:Some people might say you shouldn't put a line break into a string like that, it may not work properly for all operating systems, but the fact is it will very likely work fine in this case.

How do you know this for a fact?


WHat are you asking? Do I know what for a fact? That you can add "\n", or that some people say you shouldn't? Number one, cause I've done it a bunch of times, number two cause people have told me I shouldn't, for the reason I gave.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Fred Hamilton wrote:
pete stein wrote:
Fred Hamilton wrote:Some people might say you shouldn't put a line break into a string like that, it may not work properly for all operating systems, but the fact is it will very likely work fine in this case.

How do you know this for a fact?


WHat are you asking? Do I know what for a fact? That you can add "\n", or that some people say you shouldn't? Number one, cause I've done it a bunch of times, number two cause people have told me I shouldn't, for the reason I gave.


OK I see what you are saying. There are two issues here. There is an issue of line wrap, but there should also an issue of preserving line breaks, with the OP's first solution.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2203
    
    7
What was wrong with Michael's answer 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.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Welcome to JavaRanch, Rob.

/Pete
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2203
    
    7
pete stein wrote:Welcome to JavaRanch, Rob.

/Pete


Thanks. Just so I know who I'm talking to are you "Encephalopathic" from the Sun forums. I know he likes to keep changing user names.
 
 
subject: How to read an entire text file and display in a textbox or text area