【问题标题】:BufferedWriter and FileWriter writing strange character instead of number to text file JavaBufferedWriter 和 FileWriter 将奇怪的字符而不是数字写入文本文件 Java
【发布时间】:2018-03-12 10:25:10
【问题描述】:

我正在为我的程序将操作计数写入文件(表示为“OpCount”),但我不断收到一个奇怪的符号而不是整数。我尝试打印 OpCount 并且它输出了我正在寻找的数字,所以它只是 BufferedWriter 做了一些奇怪的事情。这是我的代码:

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

    }
}

【问题讨论】:

  • 那是你的整数。使用String.valueOf 表示其字符串。
  • OptCount 是什么类型? - 顺便说一句:请阅读并关注Java Naming Conventions

标签: java bufferedwriter operations


【解决方案1】:
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!");
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    相关资源
    最近更新 更多