首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在遍历目录时比较文件名

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

Stack Overflow用户
提问于 2013-11-29 13:05:09
回答 1查看 2.6K关注 0票数 0

我需要比较不同扩展名的文件名,以查找同名的文件。我不关心扩展。我正在使用SimpleFileVisitor方法visitFile()遍历这些文件。我只是获取根目录名或目录名并使用walkFileTree()来获取文件,所以我不知道files.The进程的数量必须是后台进程。我想过使用线程遍历文件并匹配它们,但不知道如何实现它。那么我如何添加比较场景呢?我获取文件名的代码是:-

代码语言:javascript
复制
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;

        }



    }
EN

回答 1

Stack Overflow用户

发布于 2013-11-29 13:12:43

若要从路径中获取文件名,请执行以下操作。

代码语言:javascript
复制
File f = new File("C:\\Hello\\AnotherFolder\\The File Name.PDF");
System.out.println(f.getName());

用于删除文件扩展名

代码语言:javascript
复制
str.substring(0, str.lastIndexOf('.'));

比较文件名。

代码语言:javascript
复制
// 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20278713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档