Java将int写入文件显示" chinese"文件中的字母

时间:2015-01-11 11:59:04

标签: java file

我是新来的。 所以这是我的问题:

正如我在标题上所说的那样,我的文件中的数字以奇怪的格式显示,例如:“d∟ÿ”。

这是我的代码:

    try
    {
        int x;
        FileOutputStream out=new FileOutputStream("numbers.txt");
        DataOutputStream st=new DataOutputStream(out);

        x=readInt();
        st.writeInt(x);

        st.close();
    }
    catch(IOException e)
    {
        System.out.println("Problem with the output file");
    }

如何防止这种情况并实际查看我输入的int号?

1 个答案:

答案 0 :(得分:1)

如果您想将数字写为文本,而不是像您所做的那样将二进制数写入,请改用PrintWriter。

try (PrintWriter pw = new PrintWriter("number.txt")) {
    int x = readInt();
    pw.println(x);
}

要附加而不是重写文件,您可以

try (PrintWriter pw = new PrintWriter(new FileWriter("number.txt", true))) {
    int x = readInt();
    pw.println(x);
}
相关问题