• Post Reply Bookmark Topic Watch Topic
  • New Topic

Read a File to a Text area

 
MIRANGA_H
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i wanna read a .txt file and get the content in the file into a String variable so that i can display that in a JTextArea...

how do i do it ???
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at FileReader and BufferedReader in the JavaDoc. I think there's an example right in there of setting up to read a file. That will let you one line at a time into a string variable. Can you get from there into your text area?

If you're a little more adventurous, and the file is not megabytes long, JDK 5 has the Scanner class that can read a file with one line of code.

Take a shot at making this work, post some code here if you get stuck, let us know how it goes.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, I would suggest that you try to break this into separate problems, and write code to solve each problem separately. Try to write some code to read from a file into a String variable, period. Forget about the JTextArea for now. Once you can get that part working, then bring in the JTextArea - in a separate class. Do not, under any circumstances, try to create a single class that does all this. It will be painfully overcomplicated and hard to debug. One class can convert a file to a String; another can take a String and display it in a JTextArea. After you do these two things, it should be relatively easy to hook up the two classes.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

A bit of business: you may not have read our naming policy on the way in. It requires that you use a first and last name for your display name; a single name isn't enough (although we might make an exception for Madonna if she wanted to learn Java.) You can change your display name
here.
Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!