File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes writing String data to a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "writing String data to a file" Watch "writing String data to a file" New topic
Author

writing String data to a file

Senaca Boyd
Greenhorn

Joined: Feb 21, 2007
Posts: 9
I'm familiar with writeInt, writeLong, etc., methods for writing primitive numerical data types to a file, but does Java provide a similar method for writing String data to a file?
Srikanth Ramu
Ranch Hand

Joined: Feb 20, 2007
Posts: 76
to write string into a file you can use java.io.PrintStream
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
It depends what you want. Which writeInt() etc. methods are you using - i.e. what class?

If we're talking about java.io.ObjectOutputStream class, you have two choices for writing String. You can use writeObject(), as a String is an Object. Or you can use writeUTF(), which writes the String more compactly and is specific to String data.

Note that, if you did want to write String to a text file, PrintWriter and FileWriter are usually preferred to PrintStream, because PrintStream has encoding issues (see its JavaDoc).


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
writeInt() and writeLong() could also be from a DataOutputStream or RandomAccessFile. In which case, use writeUTF().


"I'm not back." - Bill Harding, Twister
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: writing String data to a file