2017-03-15 82 views
-3

修复的空白文件IO我在Java编程新手和学习10。我正在制作一个简单的RPG游戏,但在我的代码中遇到了问题。它是关于这个文件,当我完成运行它时,它是空白的。不知何故,当我运行它时,我得到一个空文件。有人能帮助我:c。 (抱歉坏英文)如何在Java

这里是我的代码:FOR Random类

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

}

FOR字符类

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

}

主类(这里是WERE这个问题我不知道如果我缺少一些东西在这里)

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

}

+0

对不起,应该如何解决空文件IO在java中。 –

+0

@arc tinmart dayos您可以点击[编辑](http://stackoverflow.com/posts/42821879/edit)和编辑您的问题,标题等 – davedwards

+1

您需要关闭'outputStream'在程序 – AhmadWabbi

回答

0

当你打开一个流,你应该总是最后关闭它。

在java 1.7及以上版本中您只需try(... open the stream here){ ... useit ...},编译器添加了,最终隐式地关闭

您需要终于接近,以确保你不要离开分配资源开放。

将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 
    } 
+0

太好了。它帮助,但我会添加'outputStream.close();'在过去的?或'outputStream.flush();' –

+0

如果你使用的Java 7既不。 – minus