File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

writing String data to a file

Senaca Boyd

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
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 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

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
I agree. Here's the link:
subject: writing String data to a file
jQuery in Action, 3rd edition