Forums Register Login

About Opening a file

+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
im having the same problem...can anyone help us??
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thank you Rob Prime.
- Add line breaks myself.
- Use .append

I didnt realize I am using setText().

Thanks.
When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1010 times.
Similar Threads
JProgressBar shows no progress
coding a texteditor: want to open a separate option window
About Saving in a file
GUI: Cut/Copy/Paste/Etc.
MenuBar and Toolbar problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:23:33.