BufferedWriter and FileWriter writing strange character instead of number to text file Java

1.4k Views Asked by At

I am writing an operation count to a file (denoted as "OpCount") for my program but I keep getting a strange symbol instead of an integer. I tried printing OpCount instead and it outputted the number I was looking for, so its just BufferedWriter doing something strange. Here is my code:

public void writeOpToFile() {
   try   {
      BufferedWriter writer = new BufferedWriter(new FileWriter("NumberOperations.txt"));
      writer.write(OpCount);
      writer.close(); 
   } catch (IOException e)  {

   System.err.println("File was not found. Please make sure the file exists!");

    }
}
1

There are 1 best solutions below

0
Amanag On
public void writeOpToFile() {
    try {
      BufferedWriter writer = new BufferedWriter(new FileWriter("NumberOperations.txt"));
      writer.write(new Integer(OpCount).toString());
      writer.close();
    } catch (IOException e)  {

        System.err.println("File was not found. Please make sure the file exists!");
    }
}