Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Saving a string to a file

Thread view: 
jay - 30 Jan 2006 03:07 GMT
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_DIRECTORIES);
           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.
Roedy Green - 30 Jan 2006 04:15 GMT
>FileOutputStream fStream = new
>FileOutputStream(SF);
[quoted text clipped - 5 lines]
>                    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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Venky - 30 Jan 2006 04:57 GMT
May be you can do this way:

File file = fc.getSelectedFile();
FileOutputStream fStream = new FileOutputStream(file);
fstream.write(s.getBytes());
fstream.close();
jay - 30 Jan 2006 05:40 GMT
Thanks for your replies, they were definitively a big help.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.