我有以下情况:
我认为问题是主文件无法删除,但为什么?
public void defaultGameStart() {
this.getServer().getScheduler()
.scheduleSyncRepeatingTask(this, new Runnable() {
public void run() {
setCountdownLess();
}
}, 20, 20);
}
public void setCountdownLess() {
File zuÄnderndeDatei = new File("plugins/DB/join_signs.txt");
try {
BufferedReader reader = new BufferedReader(new FileReader(
zuÄnderndeDatei));
String line;
while ((line = reader.readLine()) != null) {
String[] spLine = line.split(";");
if (spLine[2].startsWith("BE")) {
File zuÄnderndeDatei11 = new File("plugins/DB/arenas/arenas_"+spLine[2].toLowerCase()+".txt");
File buffer = new File("plugins/DB/arenas/f1.txt");
try {
BufferedReader reader1 = new BufferedReader(new FileReader(
zuÄnderndeDatei11));
BufferedWriter writer = new BufferedWriter(new FileWriter(buffer));
String line1;
System.out.println("-> "+spLine[2]);
line1 = reader1.readLine();
System.out.println("-> "+line1);
writer.write((Integer.parseInt(line1) + 1)+"");
writer.write(System.getProperty("line.separator"));
countDownToStart((Integer.parseInt(line1) + 1),
spLine[2]);
System.out.println("-> "+(Integer.parseInt(line1) + 1));
reader1.close();
writer.flush();
writer.close();
zuÄnderndeDatei11.setWritable(true);
if (zuÄnderndeDatei11.delete()) {
buffer.renameTo(zuÄnderndeDatei11);
}
} catch (Exception e) {
System.out
.println("Fehler beim Anpassen der Template-Datei<>setcountdownless");
e.printStackTrace();
}
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
顺便说一句,这是一个Bukkit插件。
答案 0 :(得分:1)
if (zuÄnderndeDatei11.delete()) {
buffer.renameTo(zuÄnderndeDatei11);
}
正在返回false
因为在尝试删除之前没有关闭阅读器。如果在您尝试删除文件之前没有关闭阅读器,系统将不会删除它,因为它认为用户仍然打开了文件(您这样做,但通过java)。你需要关闭阅读器,然后尝试删除它。
编辑:以及为什么要通过文件创建倒计时?这是如此低效,它会让我哭泣。用这个:
final int time = 90;
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask()
{
@Override
public void run()
{
if(time > 0)
time--;
else
{
timer.cancel();
//do something
}
}
},1000,1000);