aspose file tools*
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
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "About Opening a file" Watch "About Opening a file" New topic
Author

About Opening a file

eLL Pascual
Greenhorn

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:

code:
----------------------------------------------------------------------------

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

if(source.getText().equalsIgnoreCase("open")){
try{
FileInputStream fi=new FileInputStream(openfile());
BufferedReader br=new BufferedReader(new InputStreamReader(fi));
String s01=br.readLine();
if(s01 != null) textArea.setText(s01);
br.close();
fi.close();
}
catch(Exception ex){}
}
}

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

----------------------------------------------------------------------------

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

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

Joined: Mar 22, 2005
Posts: 41885
    
  63
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
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
eLL Pascual
Greenhorn

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

I didnt realize I am using setText().

Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Opening a file