gpt4 book ai didi

java - 如何修复 content.Equals 的错误输出?

转载 作者:行者123 更新时间:2023-11-30 07:40:01 25 4
gpt4 key购买 nike

我试图让 Java 读取目录中的所有文件并将文件名与字符串列表进行比较,如果文件名与列表中的字符串相同,它应该输出文件名 +“命中”。现在我只命中了一个文件。

命中的文件夹包含:照片5.jpegyH1laMN0s7g.jpegRdrzTHAvcQg.jpeg

列表 lijst.txt 包含:照片5.jpegyH1laMN0s7g.jpegRdrzTHAvcQg.jpeg

所以我应该得到:foto5 命中!RdrzTHAvcQg 命中!yH1laMN0s7g 命中!

但我现在得到的是:照片5 *RdrzTHAvcQg 命中!yH1laMN0s7g *

我试过编码,现在是 UTF-8。但我不认为这是问题所在。

 public static void main(String[] args) {
// TODO Auto-generated method stub
String hit = ""; // empty string

File files = new File("C:\\Users\\bram\\Pictures\\hit");
File[] sourceFiles = files.listFiles(); // the files in the map hit java reads

List<String> lines = new ArrayList<String>();
try {
lines = FileUtils.readLines(new File("C:\\lijst.txt"), "utf-8");
} catch (IOException e2) {
} // java reads strings in the list

for (File x: sourceFiles) { // java iterates through all the files in the folder hits.

String names = x.getName().replaceAll("\\.[^.]*$", "").toString();
// java gets the filenames and converts them to strings without the extension

for (String a : lines) // for all strings in the list:
{
//System.out.println(a);
if(names.contentEquals(a)) // if the name is equel to one of the strings in the list it generates a hit
{
hit = "Hit!";
}else {
hit = "*"; // no hit is *
}
}

System.out.println(x.getName().replaceAll("\\.[^.]*$", "").toString() +" "+ hit); // print the results

}

}

}

最佳答案

你把事情搞得太复杂了。尽管您的 txt 文件包含图像文件的全名,但为什么要删除文件扩展名?为什么要嵌套 for 循环?像下面这样的东西还不够吗?

for (File x : sourceFiles) {            
if(lines.contains(x.getName())){
System.out.println(x.getName()+"\t Hit!");
}
else{
System.out.println(x.getName()+"\t *");
}
}

关于java - 如何修复 content.Equals 的错误输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58452324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com