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
?
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.
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.
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.
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.
You have to write String...
you can try.
wr.write("123");
OR
wr.write(new Integer(123).toString());
OR
wr.write( String.valueOf(123) );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With