读取当前工作空间文件名:
代码:
1 package io; 2 3 import java.util.regex.*; 4 import java.io.*; 5 import java.util.*; 6 7 public class DirList2 { 8 public static FilenameFilter filter(final String regex){ 9 return new FilenameFilter(){ 10 private Pattern pattern = Pattern.compile(regex); 11 public boolean accept(File dir,String name){ 12 return pattern.matcher(name).matches(); 13 } 14 }; 15 } 16 public static void main(String[] args){ 17 File path = new File("."); 18 String [] list; 19 if(args.length ==0) 20 list = path.list(); 21 else 22 list = path.list(filter(args[0])); 23 Arrays.sort(list,String.CASE_INSENSITIVE_ORDER); 24 for(String Item: list) 25 System.out.println(Item); 26 } 27 }
结果:
1 .classpath 2 .project 3 .settings 4 bin 5 DirList2.java 6 src