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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "About Saving in a file" Watch "About Saving in a file" New topic

About Saving in a file

eLL Pascual

Joined: May 22, 2008
Posts: 21
Guys please help me with this...

I made an application that will save the texts in my text area in a text file but the problem is it is saving the texts in a straight line.

This this is how it looks when it is in my text area:

Harry Potter

But there are squares positioned between the words when you open the text file:

Cedric[]Diggory[]Harry Potter(something like this!)

What seems to be the problem?

This is my source code:

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

else if(source.getText().equalsIgnoreCase("save as")){
FileOutputStream fos=new FileOutputStream(writefile());
PrintStream ps=new PrintStream(fos);
catch(Exception ex){}


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

Well, I guess this is all you need to see.

And what should I do to make the file extension name fixed in .txt and not all type?

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
I think JTextArea uses a newline character to separate lines. So if you're on an OS that uses a different line separator (like Windows) you'd need to adapt that when saving to a file. Inserting a carriage return character before each newline character might do the trick.
I agree. Here's the link:
subject: About Saving in a file
It's not a secret anymore!