简体   繁体   English

BufferedWriter和FileWriter将奇怪的字符而不是数字写入文本文件Java

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

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. 我正在为程序编写一个操作计数到一个文件(表示为“ OpCount”),但是我不断收到一个奇怪的符号,而不是整数。 I tried printing OpCount instead and it outputted the number I was looking for, so its just BufferedWriter doing something strange. 我试着打印OpCount,它输出了我想要的数字,所以它只是BufferedWriter做一些奇怪的事情。 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!");

    }
}
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!");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM