Meaningless Drivel is fun!*
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "About Saving in a file" Watch "About Saving in a file" New topic
Author

About Saving in a file

eLL Pascual
Greenhorn

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:

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
Marshal

Joined: Mar 22, 2005
Posts: 41060
    
  43
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.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Saving in a file
 
Similar Threads
Why isn't my code Opening a Saved file?
How to save properly?
About Menu Items
About Opening a file
MenuBar and Toolbar problem