• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Opening a file

 
eLL Pascual
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im having the same problem...can anyone help us??
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
eLL Pascual
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rob Prime.
- Add line breaks myself.
- Use .append

I didnt realize I am using setText().

Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic