0
我需要將播放器數據寫入文件並讀取它,並且每當它保存時編輯數據。我對File I/O不太好,但是這裏是我的。重置文件數據
public class DirectoryMaker {
static BufferedWriter bw;
static int lvl = 1;
static int hp = 100;
public static void main(String[] args) throws IOException {
FileWriter fw;
File dir = new File("c:\\MyFile\\b\\stuff");
if (!dir.exists()) {
if (dir.mkdirs()) {
System.out.println("Directory is created!");
} else {
System.out.println("Failed to create directory!");
}
}
String fileName="hi";
File tagFile=new File(dir,fileName+".txt");
if(!tagFile.exists()){
try {
fw = new FileWriter(tagFile.getAbsoluteFile());
bw = new BufferedWriter(fw);
bw.write("LEVEL: " + lvl + "\n");
tagFile.createNewFile();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Scanner s = new Scanner(tagFile);
while(s.hasNextLine()) {
System.out.println(s.nextLine());
}
System.exit(1);
}
}
使用此代碼,當它更改時,如何更新文件中的lvl int。這個int更新後,我需要添加其他人並閱讀它們。
爲什麼重寫整個文件不是一個選項? – 2014-09-06 19:03:23