(Java)Reader一直在无缘无故地删除文本

(Java)Reader一直在无缘无故地删除文本,java,file,text,random,Java,File,Text,Random,我犯了一个非常恼人的错误。我已经做了好几天了,还有大量的研究都没有成功。基本上,代码所做的是读取文件,但似乎在读取后删除了文本。只有某些行。这是我的读者方法: synchronized public static String getValue(String path, String filename, Boolean useColors, String folder){ try{ while(!TxtWriter.isResting()); FileRe

我犯了一个非常恼人的错误。我已经做了好几天了,还有大量的研究都没有成功。基本上,代码所做的是读取文件,但似乎在读取后删除了文本。只有某些行。这是我的读者方法:

synchronized public static String getValue(String path, String filename, Boolean useColors, String folder){
    try{
        while(!TxtWriter.isResting());
        FileRequest req =  writer.getRelitive(filename, path);
        if(req==null){
            File f = new File(Conquest.FilePaths+"/"+folder, filename);
            if(!f.getParentFile().exists())f.getParentFile().mkdirs();
            if(f.exists()){
                BufferedReader in = new BufferedReader(new FileReader(f));
                String a;
                while((a=in.readLine())!=null){
                    if(a.startsWith("'"+path+"'"+":")){
                        in.close();
                        a=a.substring(path.length()+3);
                        if(useColors)a=colorCoder(a);
                        return a;
                    }
                }
                in.close();
            }else TxtWriter.createFile(filename, folder);
        }else return ((useColors)?colorCoder(req.getMessage()):req.getMessage());
    }catch(Exception e){
        System.err.println("[Conquest/TxtReader] Error reading file "+filename+"!");
        e.printStackTrace();
    }
    return null;
}
这是读取之前的文件:

“王国”:德文内尔

“标志”:引线

我称之为方法

public String getPlayerKingdom(String p){
    String in = TxtReader.getValue("Kingdom", p+".txt", false, "Citizens");
    return ((in==null)?in:"None");
}
它返回“None”,文件现在只有
“标志”:Leader
,没有其他内容

真正让我困惑的是,某些方法是有效的,而其他方法则不然。即使它们是完全相同的代码,但具有不同的字符串。这里的这个方法是一样的,但是它的工作原理和它应该的一样

public int getResource(String kingdom, String name){
    String out = TxtReader.getValue("R-" + name, kingdom + ".txt", false, "Kingdoms");
    if(out==null)return 0;
    return Integer.valueOf(out);
}
如果有人能帮忙,我将不胜感激

这行看起来不对:

return ((in==null)?in:"None");
难道不是相反吗

return ((in==null)?"None":in);
正因为这个原因,我不喜欢三元运算符结构。只需使用if/else块,并清楚地知道您在做什么

此外,BufferedReader不会从文件中删除信息。时期它只读取信息,而不读取其他内容。如果删除了一行代码,那么程序中可能存在另一个bug,可能是代码中未显示的bug导致的

但是,不管错误的来源是什么,您的代码看起来效率很低,而且容易出错。为什么每次需要检查匹配项时都要重新读取文件?为什么不简单地读入一次文件,然后将信息存储在
HashMap