在java中以特定字母开头的文件列表 [英] List of files starting with a particular letter in java
问题描述
我在@的相对目录(运行的应用程序目录)中有一些文件,我需要在java中打开它们。告诉我一个方法来实现它。如果有帮助,我正在netbeans上工作。他们基本上是.ser文件。所以我必须获取它们中的对象
File dir = new File( 。);
if(!dir.isDirectory())抛出新的IllegalStateException(wtf mate?);
for(File file:dir.listFiles()){
if(file.getName()。startsWith(@))
process(file);
$ b $ p
$ b 重新审视后,事实证明你还可以做其他事情。注意我使用的文件过滤器。
import java.io.File;
class Test {
public static void main(String [] args){
File dir = new File(。);
if(!dir.isDirectory())抛出新的IllegalStateException(wtf mate?);
(文件file:dir.listFiles(new RegexFileFilter(@ * \\.ser))){
process(file);
public static void process(File f){
System.out.println(f.getAbsolutePath());
$ / code $ / $ p
这是我使用的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();
}
}
结果如下。注意三个好文件和三个坏文件。如果你不得不这样做,我建议使用这个,特别是如果你需要基于文件的其他属性,如文件名,如长度,修改日期等。
C:\junk\j> dir
驱动器C中的卷没有标签。
批量序列号是48FA-B715
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 bad@file.ser
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测试
@ first.ser
@ second.ser
@ third.ser
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
解决方案 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
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 bad@file.ser
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
这篇关于在java中以特定字母开头的文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!