Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Saving in a file

 
eLL Pascual
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

Cedric
Diggory
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:

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

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

}

public String writefile(){
FileDialog fd=new FileDialog(new Frame(),"File Save",FileDialog.SAVE);
fd.show();
String fullpath=fd.getDirectory()+fd.getFile();
fd.dispose();
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?

Thanks!
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic