Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write int to text file using Writer

Tags:

java

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?

like image 888
Sergey Metlov Avatar asked Sep 09 '11 06:09

Sergey Metlov


People also ask

Can you write an int to a file in Java?

FileWriter: FileWriter is the simplest way to write a file in Java. It provides overloaded write method to write int, byte array, and String to the File. You can also write part of the String or byte array using FileWriter.

How do you convert a number to a text file in Java?

The simplest way to write text to a file requires us to use PrintWriter class from the standard package java.io . The class PrintWriter has the familiar print() and println() methods we have been using for writing to the console.

What is the process in Java for creating and writing a text file?

Creating and Writing a File by Using Stream I/Omethod. This method opens or creates a file for writing bytes and returns an unbuffered output stream. The method takes an optional OpenOption parameter. If no open options are specified, and the file does not exist, a new file is created.

What class gives an easy way to write data to files?

Java FileWriter class is used to write character-oriented data to a file. It is a character-oriented class that is used for file handling in java.


1 Answers

You have to write String...

you can try.

wr.write("123");

OR

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

OR

wr.write( String.valueOf(123) );
like image 108
Talha Ahmed Khan Avatar answered Sep 19 '22 16:09

Talha Ahmed Khan