gpt4 book ai didi

java - 使用通配符 (*) 匹配的递归文件搜索

转载 作者:太空宇宙 更新时间:2023-11-04 07:13:43 25 4
gpt4 key购买 nike

我编写此代码来搜索文件:

package filesearch;

import java.io.File;

public class FileSearch {

public void walk(String path, String partOfFile) {
File root = new File(path);
File[] list = root.listFiles();

if (list == null)
return;

for (File f : list) {
if (f.isDirectory()) {
walk(f.getAbsolutePath(), partOfFile);
} else {
if (f.getAbsolutePath().contains(partOfFile)) {
System.out.println("File:" + f.getAbsoluteFile());
}
}
}
}

public static void main(String[] args) {
FileSearch fw = new FileSearch();
fw.walk("g:\\", "abs");
}
}

我想使用通配符(*)。那么 if(*) 是模式的第一个或最后一个字符我可以通过添加以下代码来实现它:

if(f.getAbsolutePath().startsWith(partOfFile)){
System.out.println( "File:" + f.getAbsoluteFile() );
}
if(f.getAbsolutePath().endWith(partOfFile)){
System.out.println( "File:" + f.getAbsoluteFile() );
}

当我有多个通配符 (*) 时以及当通配符 (*) 位于模式中间时,我该如何处理?

最佳答案

试试这个程序。

import java.io.File;

public class FileSearch
{

public void walk( String path, String partOfFile )
{
File root = new File( path );
File[] list = root.listFiles();
if ( list == null )
return;

for ( File f : list )
{
if ( f.isDirectory() )
{
walk( f.getAbsolutePath(), partOfFile );
}
else
{
boolean isFileMatched = isFileMatched( f, partOfFile);
if ( isFileMatched )
System.out.println( "File:" + f.getAbsoluteFile() );
}
}
}

private boolean isFileMatched( File file, String partOfFile )
{
boolean isMatched = true;
if ( file == null || partOfFile == null )
return false;

String fileName = file.getName();

//to ignore extension
if ( fileName.contains( "." ) )
fileName = fileName.substring( 0, fileName.lastIndexOf( "." ) );

String[] tokens = null;
if ( partOfFile.contains( "*" ) )
{
if ( partOfFile.startsWith( "*" ) && partOfFile.endsWith( "*" ) )
{
tokens = partOfFile.split( "\\*" );
isMatched = isTokenMatched( isMatched, fileName, tokens );
}
else if ( partOfFile.startsWith( "*" ) )
{
String suffix = partOfFile.substring( partOfFile.lastIndexOf( "*" ) + 1 );
if ( fileName.endsWith( suffix ) )
{
tokens = partOfFile.split( "\\*" );
isMatched = isTokenMatched( isMatched, fileName, tokens );
}
else
isMatched = false;
}
else if ( partOfFile.endsWith( "*" ) )
{
String prefix = partOfFile.substring( 0, partOfFile.indexOf( "*" ) );
if ( fileName.startsWith( prefix ) )
{
tokens = partOfFile.split( "\\*" );
isMatched = isTokenMatched( isMatched, fileName, tokens );
}
else
isMatched = false;
}
else
{
if ( !fileName.equals( partOfFile ) )
isMatched = false;
}
}
return isMatched;
}

private boolean isTokenMatched( boolean isMatched, String fileName, String[] tokens )
{
if ( tokens != null )
{
for ( String token : tokens )
{
if ( !fileName.contains( token ) )
{
isMatched = false;
break;
}
}
}
return isMatched;
}

public static void main( String[] args )
{
FileSearch fw = new FileSearch();
fw.walk("g:\\" , "abs");
}
}

关于java - 使用通配符 (*) 匹配的递归文件搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20287274/

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