File 类
File 类:它既可以代表一个特定文件的名字,也可以代表目录内一系列文件的名字。若代表一个文件集,便可用list()方法查询这个集,返回的是一个字串数组。
把File看成某个路径下的一系列东西
File的构造函数为:File file = new File(“某个路径”);
就是根据某个路径新建File对象
例1:查看当前目录下有啥文件或文件夹
import java.util.regex.*;
import java.io.File;
import java.io.FilenameFilter;
import java.util.*;
public class DirList {
public static void main(String[] args) {
// ”.”或”.\”代表当前目录,这个目录也就是jvm启动路径,可以打印出来看看路径
File file = new File(".");
System.out.println(file.getAbsolutePath());
String[] list;
if(args.length == 0)
list = file.list();
else
list = file.list(new DirFilter(args[0]));
// 匹配任意字符
//list = file.list(new DirFilter("[\\d\\D]*"));
// 匹配以b开头的文件夹或文件
//list = file.list(new DirFilter("^b.*"));
Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
for(String dirItem : list)
System.out.println(dirItem);
}
}
class DirFilter implements FilenameFilter {
private Pattern pattern;
public DirFilter(String regex) {
pattern = Pattern.compile(regex);
}
// 判断某个name是否匹配正则表达式
public boolean accept(File dir, String name) {
return pattern.matcher(name).matches();
}
}
结果:
D:\Java\eclipse_se\lifan_workspace\firstProject\.
.classpath
.project
.settings
bin
mylib
src
注:
自己写了一个正则表达式过滤器,当用它过滤时,比如只找以b开头的文件(夹)
list = file.list(new DirFilter("^b.*"));
最后只会得到 bin这一个文件夹
精简写法:
import java.util.regex.*;
import java.io.*;
import java.util.*;
public class DirList3 {
public static void main(final String[] args) {
File path = new File(".");
String[] list;
if(args.length == 0)
list = path.list();
else
list = path.list(new FilenameFilter() {
private Pattern pattern = Pattern.compile(args[0]);
public boolean accept(File dir, String name) {
return pattern.matcher(name).matches();
}
});
Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
for(String dirItem : list)
System.out.println(dirItem);
}
}
有关File的一些操作
例如
File f = new File(“…”);
创建文件夹 f.mkdirs();
删除文件(夹) f.delete();
例2:
import java.io.*;
public class MakeDirectories {
private static void usage() {
System.err.println(
"Usage:MakeDirectories path1 ...\n" +
"Creates each path\n" +
"Usage:MakeDirectories -d path1 ...\n" +
"Deletes each path\n" +
"Usage:MakeDirectories -r path1 path2\n" +
"Renames from path1 to path2");
System.exit(1);
}
// 查看file的信息
private static void fileData(File f) {
System.out.println(
"Absolute path: " + f.getAbsolutePath() +
"\n Can read: " + f.canRead() +
"\n Can write: " + f.canWrite() +
"\n getName: " + f.getName() +
"\n getParent: " + f.getParent() +
"\n getPath: " + f.getPath() +
"\n length: " + f.length() +
"\n lastModified: " + f.lastModified());
if(f.isFile())
System.out.println("It's a file");
else if(f.isDirectory())
System.out.println("It's a directory");
}
public static void main(String[] args) {
if(args.length < 1) usage();
// 重命名
if(args[0].equals("-r")) {
if(args.length != 3) usage();
File
old = new File(args[1]),
rname = new File(args[2]);
old.renameTo(rname);
fileData(old);
fileData(rname);
return; // Exit main
}
int count = 0;
boolean del = false;
if(args[0].equals("-d")) {
count++;
del = true;
}
count--;
while(++count < args.length) {
File f = new File(args[count]);
if(f.exists()) {
System.out.println(f + " exists");
if(del) {
System.out.println("deleting..." + f);
f.delete();
}
}
else { // Doesn't exist
if(!del) {
f.mkdirs();
System.out.println("created " + f);
}
}
fileData(f);
}
}
}