Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Saving a string to a file

Reply
Thread Tools

Saving a string to a file

 
 
jay
Guest
Posts: n/a
 
      01-30-2006
Hi all,
I am trying to save a string that I get from a JTextArea like this:

---------
String s = textArea.getText();
---------

After getting the text I save it using the following code

---------------Simplified to reduce number of lines----------
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIR ECTORIES);
int returnVal = fc.showSaveDialog(frame);
try
{
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
SF = (file.toString() + ".txt");
FileOutputStream fStream = new
FileOutputStream(SF);
ObjectOutputStream stream = new
ObjectOutputStream(fStream);
stream.writeObject(s);
//stream.writeBytes(s);
stream.flush();
stream.close();
fStream.close();
}
}catch (Exception e) {
JOptionPane op = new JOptionPane();
op.showMessageDialog(null,"A document writing error has
occured");
}
-----------------------------------------------------------------------
This will save everything correctly but it adds some additional
characters in front of the file.
That is if the string 's' contains: this is a test file
After saving it the file itself has some unreadable characters in front
of the actual text.

Does anyone have any ideas of why these characters are being added to
the string once I save it to a file I have tried both
stream.writeObject(s) and stream.writeBytes(s) but they both add the
extra characters.

I know that the string 's' doesn't contain the extra characters because
I print it just before saving and it prints as expected.

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      01-30-2006
On 29 Jan 2006 19:07:25 -0800, "jay" <> wrote,
quoted or indirectly quoted someone who said :

>FileOutputStream fStream = new
>FileOutputStream(SF);
> ObjectOutputStream stream = new
>ObjectOutputStream(fStream);
> stream.writeObject(s);
> //stream.writeBytes(s);
> stream.flush();
> stream.close();
> fStream.close();


this is using a flame thrower to kill a fly. You could do that more
easily with a DataOutputStream if you don't intend anyone to read it
or a FileWriter if you do. See
http://mindprod.com/applets/fileio.html
for sample code.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
 
Venky
Guest
Posts: n/a
 
      01-30-2006
May be you can do this way:

File file = fc.getSelectedFile();
FileOutputStream fStream = new FileOutputStream(file);
fstream.write(s.getBytes());
fstream.close();

 
Reply With Quote
 
jay
Guest
Posts: n/a
 
      01-30-2006
Thanks for your replies, they were definitively a big help.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Saving a binary file into a string Rune Allnor C++ 3 01-15-2010 02:02 PM
java saving String to file jameson737 Java 0 09-29-2006 10:13 AM
EXCEL question saving a file saving the the first column as read only Luis Esteban Valencia ASP .Net 0 01-06-2005 07:02 PM
Saving DataTable to session vs saving a Custom object. John Kandell ASP .Net 4 12-10-2004 05:08 AM
Saving Images While Saving ASP Pages ! Lovely Angel For You ASP General 1 10-03-2003 12:03 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57