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

Sergey Metlov
  • 25,747
  • 28
  • 93
  • 153

8 Answers8

28

You have to write String...

you can try.

wr.write("123");

OR

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

OR

wr.write( String.valueOf(123) );
Talha Ahmed Khan
  • 15,043
  • 10
  • 42
  • 49
  • Why do I have to make the int 123 an object type of Integer? Dosent java already know that this is an int value and therefore should be able to translate that to string? Whats the difference between Obj. of Integer and datatype of int? – Ashwin Gupta Sep 07 '15 at 00:38
  • 2
    @AshwinGupta and anyone else wondering, it's because the method `wr.write(int)` doesn't actually write the int itself, it writes the character represented by the `int` in the encoding specified (utf-8 if not specified). So the int's are mapped to other characters, making it an object and getting the string of it forces it to write the actual integer down, else autoboxing or unboxing will convert it back to a primitive int. See: https://docs.oracle.com/javase/7/docs/api/java/io/Writer.html#write(int) – matrixanomaly Apr 18 '16 at 05:31
5

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.

Arash Saidi
  • 2,228
  • 20
  • 36
4

Javadocs are your friend:

Convenience class for writing character files.

} is the character with the ASCII value 123.

If you want to write the text representation of 123 to this file, you'd want to do:

wr.write(Integer.toString(123));
Brian Roach
  • 76,169
  • 12
  • 136
  • 161
3

I have had a good experience in the past with PrintStream. PrintStream can be used in two ways (at least)

PrintStream ps = new PrintStream("file_location.txt");

OR

PrintStream ps = new PrintStream(new File("file_loc.txt"));

and to write, use:

ps.println("String text");

OR

ps.println(Integer.parseInt(123));
peter_the_oak
  • 3,529
  • 3
  • 23
  • 37
3

123 = 0x7b = ascii code for {, so you are writing the character to the file. Use the toString() method to convert the value to a string and output it.

rickythefox
  • 6,601
  • 6
  • 40
  • 62
2

The problem is that Writer.write is used to send a single character to the output. It's not clear why it's int rather than char, admittedly - it would make sense if you could use this to send non-BMP characters, but the documentation states:

Writes a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.

You need to convert your integer to a string first, e.g.

int v = 123; // I'm assuming this is dynamic in reality
wr.write(String.valueOf(v));
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

Yet one method:

wr.write(""+123); //immediately convert int to its String representation

or

wr.write(""+i);

But I don't know, how it affect in perfomance versus other methods, but it is a little simpler to write. :)

PS: Sorry for my may be bad English

MikhailSP
  • 3,233
  • 1
  • 19
  • 29
1

This is what you are trying. See this.

http://download.oracle.com/javase/1.4.2/docs/api/java/io/Writer.html#write(int)

This is the method name in the javadoc im reffering to, incase if the link doesn't take you automatically.

public void write(int c)
       throws IOException

So, the equivalent ascii character for 123 is {

Thanks

Jayy
  • 2,368
  • 4
  • 24
  • 35