我正在将一个简单的实用程序应用程序从C#移植到Java中,作为其中的一部分,我正在为我更喜欢C#语义的项目编写一些辅助方法。一旦这种情况是Directory.GetFiles
。
import java.io.File;
import java.io.FilenameFilter;
public class Directory {
public static String[] GetFiles(String path) {
File directory = new File(path);
return directory.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
return new File(dir, filename).isFile();
}
});
}
}
虽然以上所有内容似乎都很好,并且复制了一个GetFiles重载,但我仍然坚持如何最好地编写一个复制C#Directory.GetFiles(String, String)
功能的方法。
此方法应使用path
字符串以及searchPattern
,该字符串仅用于返回与该特定模式匹配的文件。
例如,以下各项都应该有效:
// Used to get all JavaScript files
Directory.GetFiles("~/Documents/", "*.js");
// Get all CSS files in the styles sub-folder.
Directory.GetFiles("~/Documents/", "styles/*.css");
答案 0 :(得分:1)
您可以通过在每个星号和问号前面放置一个句点来修改模式,然后将其用作正则表达式来确定应返回哪些文件。所以,你可以写一些类似
的东西public static String[] GetFiles(final String path, final String searchPattern) {
final Pattern re = Pattern.compile(searchPattern.replace("*", ".*").replace("?", ".?"));
return new File(path).list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return new File(dir, name).isFile() && re.matcher(name).matches();
}
});
}
答案 1 :(得分:0)
以Adam Michalcin的答案为基础,我提出了以下内容(注意这些方法会返回一系列绝对路径:
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Pattern;
public class Directory {
public static String[] GetFiles(String path) {
File directory = new File(path);
File[] matchingFiles = directory.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
return new File(dir, filename).isFile();
}
});
ArrayList<String> files = new ArrayList<String>();
for (File file : matchingFiles) {
files.add(file.getAbsolutePath());
}
return (String[])files.toArray(new String[files.size()]);
}
public static String[] GetFiles(String path, final String searchPattern) {
// Split the searchPattern incase we have directories in there
ArrayList<String> pattern = new ArrayList<String>(Arrays.asList(searchPattern.split("/")));
// Take the last element out from the array, as this will be the file pattern
String filePattern = pattern.remove(pattern.size() - 1);
// Insert the base path into the remaining list
pattern.add(0, path);
// Now lets concat the lot to create a base path
path = Path.combine((String[])pattern.toArray(new String[pattern.size()]));
final Pattern regEx = Pattern.compile(filePattern.replace("*", ".*").replace("?", ".?"));
File directory = new File(path);
File[] matchingFiles = directory.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
return new File(dir, filename).isFile() && regEx.matcher(filename).matches();
}
});
ArrayList<String> files = new ArrayList<String>();
for (File file : matchingFiles) {
files.add(file.getAbsolutePath());
}
return (String[])files.toArray(new String[files.size()]);
}
}
这也使用了我已经实施的另一个帮助者(Path.combine
):
import java.io.File;
public class Path {
public static String combine(String... paths)
{
File file = new File(paths[0]);
for (int i = 1; i < paths.length ; i++) {
file = new File(file, paths[i]);
}
return file.getPath();
}
}
<强>用法强>
Directory.GetFiles("/Users/beardtwizzle/", "/Documents/javascripts/*.js");
这仍然是布鲁诺席尔瓦提到的另一个角色问题。因此,我也会尝试为此修复。