如何修复java中的空白文件IO

如何修复java中的空白文件IO,java,io,Java,Io,我是java编程和学习Io的新手。我正在制作一个简单的RPG游戏,但是我的代码中有一个问题。是关于文件的,每次我运行完都是空白的。不知怎的,每当我运行它,我就会得到一个空文件。有人能帮我吗。对不起,英语不好 这是我的代码:随机类 } 字符类 }当您打开一条流时,您应该始终最终关闭它 在Java1.7及更高版本中,您只需尝试。。。在这里打开流{…useit…},编译器隐式添加finally close 您需要最终关闭,以确保不会留下已分配的开放资源 明智的做法是将PrintWriter设置为自动刷

我是java编程和学习Io的新手。我正在制作一个简单的RPG游戏,但是我的代码中有一个问题。是关于文件的,每次我运行完都是空白的。不知怎的,每当我运行它,我就会得到一个空文件。有人能帮我吗。对不起,英语不好

这是我的代码:随机类

}

字符类


}

当您打开一条流时,您应该始终最终关闭它

在Java1.7及更高版本中,您只需尝试。。。在这里打开流{…useit…},编译器隐式添加finally close

您需要最终关闭,以确保不会留下已分配的开放资源

明智的做法是将PrintWriter设置为自动刷新或确保在关闭流之前刷新,如果不是这样,则PrintWriter在is缓冲区满之前可能不会写入输出流

Java1.6及以下版本

    OutputStream fos = null;
    try{
        fos = new FileOutputStream("RPGOutput.txt",true);
        PrintWriter out = new PrintWriter(fos, true);

        out.println("Someting");
        out.println("...");

        // 
        out.flush();
    }catch(IOException e){
        // Manage exception
    } finally {
        try{
            if(fos!=null){
                fos.close();
            }
        }catch(IOException e){
            // Swallow exception
        }
    }
Java 1.7及以上版本

    try(OutputStream fos = new FileOutputStream("RPGOutput2.txt",true);
            PrintWriter out = new PrintWriter(fos,true);) {

        out.println("Hello");
        out.print("Hello 2");

    }  catch (IOException e) {
        // Manage exception
    }

很抱歉,它应该知道如何修复java中的空白文件IO。@arc tinmart dayos您可以单击并编辑您的问题、标题等。您需要在程序结束时关闭outputStream,我将把它放在哪里?我的while和catch之间的outputStream??哇,很好。这会有帮助,但我会添加outputStream.close;最后呢?或outputStream.flush;如果您使用java 7,则两者都不是。
     public class TestCharacter {
     public static void main(String letsPlay[]){
    PrintWriter outputStream = null;
    try{
         outputStream = new PrintWriter(new FileOutputStream("RPGOutput.txt",true));

        Scanner s = new Scanner(System.in);
    System.out.print("Enter Player 1 Character name:");
    Character p1 = new Character(s.next(),s.nextInt(),s.nextInt(),s.nextInt());
    System.out.println(p1.getName()+ "\tHAS ENTERED THE BATTLE!");
    System.out.println("Enter Player 2 Character name:");
    Character p2 = new Character(s.next(),s.nextInt(),s.nextInt(),s.nextInt());
    System.out.println(p2.getName()+ "\tHAS ENTERED THE BATTLE!");      

    int i = 1;
    do {
        outputStream.println("\nR O U N D " + i + "!");
        outputStream.print(p1.getName() + " "+"HP is");
        outputStream.println("\t" + p1.getCurrentLife() + "/" + p1.getMaxLife());
        outputStream.println("while");
        outputStream.print(p2.getName() + " " + " HP is");
        outputStream.println("\t" + p2.getCurrentLife() + "/" + p2.getMaxLife());
        outputStream.println(" ");
        p2.wound(p1.attack());
        outputStream.println(p1.getName() + " attacks " + p2.getName() + " for " + p1.getAtk() + " damage!");
        if (p2.checkDead() == true) {
            outputStream.println(p2.getName() + " lose " + p1.getName() + " has won!");
            return;
        }
        p1.wound(p2.attack());
        outputStream.println(p2.getName() + " attacks " + p1.getName() + " for " + p2.getAtk() + " damage!");
        if (p1.checkDead() == true) {
            outputStream.println(p1.getName() + " lose " + p2.getName() + " has won!");
            return;
        }
        i++;

    } while (p1.checkDead() == false || p2.checkDead() == false);
    }catch(FileNotFoundException e){
        System.out.println("Error no file" + e);
        System.exit(0);
    }
}
    OutputStream fos = null;
    try{
        fos = new FileOutputStream("RPGOutput.txt",true);
        PrintWriter out = new PrintWriter(fos, true);

        out.println("Someting");
        out.println("...");

        // 
        out.flush();
    }catch(IOException e){
        // Manage exception
    } finally {
        try{
            if(fos!=null){
                fos.close();
            }
        }catch(IOException e){
            // Swallow exception
        }
    }
    try(OutputStream fos = new FileOutputStream("RPGOutput2.txt",true);
            PrintWriter out = new PrintWriter(fos,true);) {

        out.println("Hello");
        out.print("Hello 2");

    }  catch (IOException e) {
        // Manage exception
    }