wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Writing String representation to file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Writing String representation to file" Watch "Writing String representation to file" New topic
Author

Writing String representation to file

Ahsan Bagwan
Ranch Hand

Joined: Oct 05, 2010
Posts: 153
I am using BufferedWriter class that wraps FileWriter class to store data in a file. However I am facing some issues regarding what is written.

I have this method which is passed an integer. But while writing it gets correctly written only when I am appending to a empty string using + operator.

Using an overloaded method which takes an int results in character 'a' with a diacritical mark ending up in the file.

Following is what I have so far. What is a good way of doing this?



E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 761
    
    9
BufferedWriter.write is for writing a character not an integer. Use When you call it with an int it looks up the character represented by that int value.
Ahsan Bagwan
Ranch Hand

Joined: Oct 05, 2010
Posts: 153
Thank you. Its helpful to know the cause of why the special characters are written.

Is the using of static method valueOf() the preferred way? I suppose using Integer wrapper class and running toString on it would be an overkill.
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 761
    
    9
Ahsan Bagwan wrote:Thank you. Its helpful to know the cause of why the special characters are written.

Is the using of static method valueOf() the preferred way? I suppose using Integer wrapper class and running toString on it would be an overkill.


The String.valueOf method calls the Integer.toString method. You should use a method that you think best communicates what your code is doing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing String representation to file
 
Similar Threads
Java Programming help needed
save txt file with file chooser
Write objects to a File
write to f file
Remove unwanted double quote from .csv file