实际问题将进一步解决:),谢谢。
我对 Java 相当陌生(几乎通过了 400 页的书)。
我对 API 还不是很熟悉。
这是我阅读 .txt 文件并检查是否有任何已收集的数据已存储在 .txt 文件中的最佳方法。如果是这种情况,将从数据集合中删除数据,并添加尚未在 .txt 中找到的数据。
一些变量:
public String[] names;
public int[] levels;
public int[] IDs;
public ArrayList<String> line = new ArrayList<String>();
public ArrayList<RSNPC> monsterList = new ArrayList<RSNPC>();
public ArrayList<String> monstersToAdd = new ArrayList<String>();
检查现有 .txt 文件的方法:
private void checkForLine() {
try{
// Create file
File file = new File(getCacheDirectory() + "output.txt");
RandomAccessFile out = new RandomAccessFile(file, "rw");
for(int i = 0; i < file.length(); i++){
line.add(out.readLine());
}
for(String monster : monstersToAdd){
if(line.contains(monster)){
monstersToAdd.remove(monster);
}
}
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
然后最终保存 checkForLine() 定义的信息的方法(文件中已经没有的信息):
private void saveToFile() {
try{
// Create file
BufferedWriter out = new BufferedWriter(new FileWriter(getCacheDirectory() + "output.txt"));
for(String a : monstersToAdd){
out.write(a);
out.newLine();
log("Wrote " + a + "to file");
}
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
执行顺序:
getNPCS();
getNames(monsterList);
getLevels(monsterList);
getIDs(monsterList);
combineInfo();
checkForLine();
saveToFile();
然而,问题在于它没有正确检查 .txt 文件中的信息。我可以看到,因为它只是保存一遍又一遍地观察到的任何东西,而不是整理任何东西。这是我有限的知识所能想到的唯一方法,但它没有奏效。
对于那些想知道的人:这是一个名为 RSbot 的机器人的脚本,它玩名为 RuneScape 的游戏。我实际上并没有使用机器人,但我想这样做是为了练习。
如果可以进一步清除问题,我可以粘贴整个脚本。
我非常感谢任何帮助,当然会记得选择我使用的答案(如果有人打扰帮助;))。
谢谢。