无法将文本写入文件

Rok*_*121 0 java

我想把int写入文本文件.我写了这段代码

public static void WriteInt(int i,String fileName){
    File directory = new File("C:\\this\\");
    if (!directory.exists()) {
        directory.mkdirs();
    }
    File file = new File("C\\"+fileName);
    FileOutputStream fOut = null;

    try {
        //Create the stream pointing at the file location
        fOut = new FileOutputStream(new File(directory, fileName));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    OutputStreamWriter osw = new OutputStreamWriter(fOut);
    try {

        osw.write(i);


        osw.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是在输出文件中我没有int,只有一个符号.有什么想法吗?

ala*_*mar 5

你应该使用PrintWriter.print(int)

Writer.write()输出一个字符,这就是它的用途.不要被int参数类型弄糊涂.在PrintWriter中包装你的osw,别忘了关闭它.