java java中以特定字母开头的文件列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5681999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List of files starting with a particular letter in java
提问by Mojo_Jojo
I've got some files in the relative directory (directory the app is running in) starting with '@' and I need to open all of them in java. Show me a way to accomplish it. If it helps, I'm working on netbeans.They're basically .ser files. So I have to fetch the objects in them
我在相对目录(应用程序运行的目录)中有一些以“@”开头的文件,我需要在 java 中打开所有这些文件。告诉我一个方法来完成它。如果有帮助,我正在研究 netbeans。它们基本上是 .ser 文件。所以我必须获取其中的对象
回答by corsiKa
File dir = new File(".");
if(!dir.isDirectory()) throw new IllegalStateException("wtf mate?");
for(File file : dir.listFiles()) {
if(file.getName().startsWith("@"))
process(file);
}
After revisiting this, it turns out there's something else you can do. Notice the file filter I used.
重新审视这一点后,事实证明您还可以做其他事情。注意我使用的文件过滤器。
import java.io.File;
class Test {
public static void main(String[] args) {
File dir = new File(".");
if(!dir.isDirectory()) throw new IllegalStateException("wtf mate?");
for(File file : dir.listFiles(new RegexFileFilter("@*\.ser"))) {
process(file);
}
}
public static void process(File f) {
System.out.println(f.getAbsolutePath());
}
}
Here's the RegexFileFilter I used
这是我使用的 RegexFileFilter
public class RegexFileFilter implements java.io.FileFilter {
final java.util.regex.Pattern pattern;
public RegexFileFilter(String regex) {
pattern = java.util.regex.Pattern.compile(regex);
}
public boolean accept(java.io.File f) {
return pattern.matcher(f.getName()).find();
}
}
And here's the result. Note the three good files and the three bad files. If you had to do this on a more regular basis, I'd recommend using this, especially if you need to do it based on other attributes of the file other than file name, like length, modify date, etc.
结果如下。注意三个好文件和三个坏文件。如果您必须更频繁地执行此操作,我建议您使用它,特别是如果您需要根据文件名以外的其他属性(例如长度、修改日期等)来执行此操作。
C:\junk\j>dir
Volume in drive C has no label.
Volume Serial Number is 48FA-B715
Directory of C:\junk\j
02/14/2012 06:16 PM <DIR> .
02/14/2012 06:16 PM <DIR> ..
02/14/2012 06:15 PM 0 @bad.serr
02/14/2012 06:15 PM 0 @badser
02/14/2012 06:15 PM 0 @first.ser
02/14/2012 06:15 PM 0 @second.ser
02/14/2012 06:15 PM 0 @third.ser
02/14/2012 06:15 PM 0 [email protected]
02/14/2012 06:24 PM 692 RegexFileFilter.class
02/14/2012 06:24 PM 338 RegexFileFilter.java
02/14/2012 06:24 PM 901 Test.class
02/14/2012 06:24 PM 421 Test.java
10 File(s) 2,352 bytes
2 Dir(s) 10,895,474,688 bytes free
C:\junk\j>java Test
@first.ser
@second.ser
@third.ser
回答by rodrigoap
If it helps check java.io.FileFilter
.
如果它有助于检查java.io.FileFilter
。
回答by duffymo
Yes, open a directory File, get its List of child Files using a FileFilter that only allows through those file names that you want.
是的,打开一个目录文件,使用 FileFilter 获取其子文件列表,该文件过滤器只允许通过您想要的那些文件名。
回答by Mark Burleigh
You could use java.nio.file.DirectoryStream;
你可以使用 java.nio.file.DirectoryStream;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
//Java version 7
class A {
public static void main(String[] args) throws Exception
{
// Example directory on dos system
Path dir = Paths.get("c:\a\b\");
/**
*
* Create a new DirectoryStream for the above path.
*
* List all files within this directory that begin
* with the letters A or B i.e "[AB)]*"
*
*/
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "[AB]*"))
{
// Print all the files to output stream
for(Path p: stream)
{
System.out.println(p.getFileName());
}
}
catch(Exception e)
{
System.out.println("problems locating directory");
}
}
}