即使是真,也不用if语句删除文件,但是如果没有if语句则删除

时间:2011-09-21 23:45:01

标签: java

每当我添加:

if(lines[0].equalsIgnoreCase("owner: " + sender.getName()))

该文件不会删除,但是:

System.out.println("Deleted message successfully!");

仍在运行,这意味着if语句为真。

每当我删除上面的if语句时,它都会删除该文件。

以下是代码:(不是最好的)

String lines[], strLine;
int a = 0;
String fileLoc = currentDir + "//plugins//ExtendedSigns//" + c[1] + ".txt";
LineNumberReader  lnr = new LineNumberReader(new FileReader(new File(fileLoc)));
FileInputStream in = new FileInputStream(fileLoc);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
lnr.skip(Long.MAX_VALUE);
lines = new String[1 + lnr.getLineNumber()];
while ((strLine = br.readLine()) != null)   
{
    lines[a] = strLine;
    a++;
}
if(lines[0].equalsIgnoreCase("owner: " + sender.getName()))
{
    File del = new File(currentDir + "//plugins//ExtendedSigns//" + c[1] + ".txt");
    del.delete();
    System.out.println("Deleted message successfully!");

}
else
{
    System.out.println("Deleted message unsuccessfully! You do not own it!");
}

2 个答案:

答案 0 :(得分:2)

请改为尝试:

if(del.delete()) System.out.println("Deleted message successfully!");
else System.out.println("Deleted message unsuccessfully! You do not own it!");

答案 1 :(得分:1)

也许您的文件未关闭?在比较之前,调用br.close()以便文件关闭(并且linenumberreader也应该关闭)。可能没有你的if / else语句,JVM正在关闭文件或者它不再被锁定。可能与sender有关。