This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes About Opening a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "About Opening a file" Watch "About Opening a file" New topic

About Opening a file

eLL Pascual

Joined: May 22, 2008
Posts: 21
Does anyone there know how to open a text file and append its texts in a text area? I used the ff code to do that but the texts are in one straight line.

This is my code:


public void actionPerformed(ActionEvent ae){
JMenuItem source = (JMenuItem)(ae.getSource());

FileInputStream fi=new FileInputStream(openfile());
BufferedReader br=new BufferedReader(new InputStreamReader(fi));
String s01=br.readLine();
if(s01 != null) textArea.setText(s01);
catch(Exception ex){}

public String openfile(){
FileDialog fd=new FileDialog(new Frame(),"File Select");;
String fullpath=fd.getDirectory()+fd.getFile();
return fullpath;


Or do you know what I should add in my code to get the design of the text file?
Norazanita Adnan
Ranch Hand

Joined: May 28, 2008
Posts: 44
im having the same problem...can anyone help us??
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41052
What do you mean by "in a straight line"? The code reads only a single line from the file, and then puts that in the text area. How else but in on eline would it appear? Can you give an example of what you're asking?

Ping & DNS - my free Android networking tools app
Rob Spoor

Joined: Oct 27, 2005
Posts: 19649

You'll need to loop until readLine returns null. In your code, you're only reading the first line (if any).

The readLine method removes all line breaks, so you'll have to add them yourself. Also, use JTextArea's append method, not the setText method, to add each new line plus a new line break.

How To Ask Questions How To Answer Questions
eLL Pascual

Joined: May 22, 2008
Posts: 21
Thank you Rob Prime.
- Add line breaks myself.
- Use .append

I didnt realize I am using setText().

It is sorta covered in the JavaRanch Style Guide.
subject: About Opening a file
Similar Threads
About Saving in a file
coding a texteditor: want to open a separate option window
MenuBar and Toolbar problem
JProgressBar shows no progress
GUI: Cut/Copy/Paste/Etc.