Java 在遍历目录时比较文件名

Java 在遍历目录时比较文件名,java,file-io,Java,File Io,我需要比较具有不同扩展名的文件名,以查找具有相同名称的文件。我不关心延期。我正在使用SimpleFileVisitor方法visitFile()浏览这些文件。我只是获取根目录名或目录名,并使用walkFileTree()获取文件,因此我不知道文件的数量。该过程必须是后台进程。我曾想过使用线程遍历文件并匹配它们,但没有了解如何实现它。那么如何添加比较场景??我获取文件名的代码是:- package trigger; import java.io.IOException; import java.

我需要比较具有不同扩展名的文件名,以查找具有相同名称的文件。我不关心延期。我正在使用SimpleFileVisitor方法visitFile()浏览这些文件。我只是获取根目录名或目录名,并使用walkFileTree()获取文件,因此我不知道文件的数量。该过程必须是后台进程。我曾想过使用线程遍历文件并匹配它们,但没有了解如何实现它。那么如何添加比较场景??我获取文件名的代码是:-

package trigger;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class ProcessFilePath extends SimpleFileVisitor<Path>{

        String [] str;
        String name;

        @Override 
        public FileVisitResult visitFile(Path aFile, BasicFileAttributes aAttrs) throws IOException 

        {
            str= aFile.toFile().getName().split("\\.");
             name=str[0];
             System.out.println("filename : " + name);
             return FileVisitResult.CONTINUE;

        }



    }
包触发器;
导入java.io.IOException;
导入java.nio.file.*;
导入java.nio.file.attribute.BasicFileAttributes;
公共类ProcessFilePath扩展了SimpleFileVisitor{
字符串[]str;
字符串名;
@凌驾
公共文件VisitResult visitFile(路径文件,基本文件属性aAttrs)引发IOException
{
str=aFile.toFile().getName().split(\\);
name=str[0];
System.out.println(“文件名:”+名称);
返回FileVisitResult.CONTINUE;
}
}

从路径获取文件名

File f = new File("C:\\Hello\\AnotherFolder\\The File Name.PDF");
System.out.println(f.getName());
用于删除文件扩展名

str.substring(0, str.lastIndexOf('.'));
比较文件名

// These two have the same value
new String("test").equals("test") ==> true 

// ... but they are not the same object
new String("test") == "test" ==> false 

// ... neither are these
new String("test") == new String("test") ==> false 

// ... but these are because literals are interned by 
// the compiler and thus refer to the same object
"test" == "test" ==> true 

// concatenation of string literals happens at compile time resulting in same objects
"test" == "te" + "st"  ==> true

// but .substring() is invoked at runtime, generating distinct objects
"test" == "!test".substring(1) ==> false

@user2553442…实际上我只是获取根目录名或目录名,然后使用walkFileTree()遍历它。我不知道我需要匹配的文件的确切数量是多少。因此,您的答案似乎有点混乱。您正在尝试匹配来自不同目录的文件?将文件名存储在字符串数组中,并比较文件名。@user2553442…请重新检查问题。我做了一些编辑。walkFileTree()时,您可以将当前文件名与其他文件名进行比较。@usre2553442…请清楚您在说什么。你的评论令人困惑,也不清楚。