Write int to text file using Writer

Writer wr = new FileWriter("123.txt");
wr.write(123);
wr.close();

Output file contains:
{

Where is the problem? How to write int to text file using Writer?


Solution 1:

You have to write String...

you can try.

wr.write("123");

OR

wr.write(new Integer(123).toString());

OR

wr.write( String.valueOf(123) );

Solution 2:

There is also a very simple way to write integers to file using FileWriter:

Writer wr = new FileWriter("123.txt");
wr.write(123 + "");
wr.close();

The + "" concatenates the integers with an empty string, thus parsing the integer to string. Very easy to do and to remember.