【问题标题】:how to fix blank file IO in java [duplicate]如何在java中修复空白文件IO [重复]
【发布时间】:2017-03-15 22:26:52
【问题描述】:

我是 java 编程和学习 Io 的新手。我正在制作一个简单的 RPG 游戏,但我的代码出现问题。这是关于文件,每当我完成运行它时它都是空白的。不知何故,每当我运行它时,我都会得到一个空文件。有人可以帮助我吗:c。 (抱歉英语不好)

这是我的代码:FOR RANDOM CLASS

public class Dice {
/** instance variables */
private final  Random r;

/**
 * Initializes the random number generator r
 */


public Dice() {
    r = new Random();
}

/**
 * Returns a random integer between 1 and 6 using Random r
 * @return 
 */
public int roll() {
    int dieRoll = r.nextInt(6 - 1);
    return dieRoll;
}

}

对于字符类

public class Character {

static Dice dice = new Dice();
private  String name;
private  int strength;
private  int dexterity;
private  int intelligence;
private  int maxLife;
private int currentLife;
private int atk;

public Character(){

}
public Character(String n, int s, int d, int i) {

    this.name = n;
    this.strength = s;
    this.dexterity = d;
    this.intelligence = i;
    this.maxLife = 100 + dice.roll();
    this.currentLife = maxLife;

}


/**
 * Returns a random die roll using the roll method in the Dice.java,
 * *modified by the character's strength
 */
public int attack() {
    this.atk = strength * dice.roll() + 24;
    return atk;
}

public void wound(int damage) {
    if ((currentLife - damage) <= 0) {
        this.currentLife = 0;
    } else {
        this.currentLife = currentLife - damage;
    }
}

public void heal(int heal) {
    if ((currentLife + heal) < maxLife) {
        this.currentLife = currentLife + heal;
    } else {
        this.currentLife = maxLife;
    }
}

public boolean checkDead() {
    return currentLife == 0;
}

public String getName() {
    return name;
}

public int getStrength() {
    return strength;
}

/**
 * Returns dexterity
 */
public int getDexterity() {
    return dexterity;
}

/**
 * * Returns intelligence
 */
public int getIntelligence() {
    return intelligence;
}

/**
 * Returns currentLife
 */
public int getCurrentLife() {
    return currentLife;
}

/**
 * Returns maxLife
 */
public int getMaxLife() {
    return maxLife;
}

public int getAtk() {
    return atk;
}

}

对于主要课程(这是我不知道的问题,如果我在这里缺少一些东西)

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

}

【问题讨论】:

  • 很抱歉,它应该如何修复java中的空白文件IO。
  • @arc tinmart dayos 您可以点击edit 并编辑您的问题、标题等。
  • 程序结束需要关闭outputStream
  • 我会把它放在哪里? outputStream 在我的 while 和 catch 之间??

标签: java io


【解决方案1】:

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

在 java 1.7 及更高版本中,您只需 try( ... open the stream here ){ ... useit ...},编译器会隐式添加 finally close

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

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

Java 1.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
    }

【讨论】:

  • 哇不错。它有帮助,但我会在最后添加outputStream.close(); 吗?或outputStream.flush();
  • 如果你不使用 java 7。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-20
  • 2020-02-22
  • 1970-01-01
  • 1970-01-01
  • 2019-10-21
  • 2017-10-02
  • 2020-11-20
相关资源
最近更新 更多