I tried to add the integer values in a *.txt
file but instead it is printing ASCII values.
What is wrong with my code?
public static void main(String[] args) throws IOException
{
FileWriter f1 = new FileWriter("C:\\/New folder\\/file1.txt");
Writer bw1=new BufferedWriter(f1);
int j=0;
while(j!=20)
{
j=j+1;
bw1.write(j);
bw1.write(System.getProperty( "line.separator" ));
bw1.flush();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
f1.close();
}
You're using the method in the Writer
class that takes an int
as an argument, with the lower 16 bits of the int
representing the Unicode code point, and the corresponding character being printed to the file as shown by this table:
A simple fix would be to write strings to your file instead:
bw1.write(String.valueOf(j));