我正在分析 Web 访问日志,并尝试找出访问日志中仅请求一次的所有唯一对象(任何文件或任何路径)。每次程序写入文本文件时,文本文件的内容如下所示:
/~scottp/publish.html
/~ladd/ostriches.html
/~scottp/publish.html
/~lowey/
/~lowey/kevin.gif
/~friesend/tolkien/rootpage.html
/~scottp/free.html
/~friesend/tolkien/rootpage.html
.
.
.
我想检查要写入文本文件的行是否已存在于文本文件中。换句话说,如果它确实存在于文本文件中,则不执行任何操作并跳过它并分析下一行。如果没有,则将其写入文本文件。
我尝试使用 equals
或 contains
但似乎不起作用,这是我的代码的一小部分:
// Find Unique Object that were requested only once
if (matcher3.find()) {
if(!requestFileName.equals(bw.equals(requestFileName))) {
bw.write(requestFileName);
bw.newLine();
}
}
我应该怎么做才能实际执行检查?
最佳答案
正如 @JB Nizet 评论的那样,你应该使用 Set
Set<String> set = new HashSet<String>();
BufferedReader reader = new BufferedReader(new FileReader(new File("/path/to/yourFile.txt")));
String line;
while((line = reader.readLine()) != null) {
// duplicate
if(set.contains(line))
continue;
set.add(line);
// do your work here
}
关于java - 检查要写入文本文件的字符串/行是否已存在于文本文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39814793/