This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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: 158
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: 892
    
    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: 158
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: 892
    
    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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Writing String representation to file