1、介绍
文件系统由文件夹和文件组成,文件夹用来表示文件的层级关系,而文件是流的一种载体。
我们使用的操作系统要么是 Windows,要么是 Linux。
所以,学习流操作之前,先学习文件和目录的操作。
File 对象集文件操作和目录操作于一身。
2、File 对象说明
1)、实例化File
测试类
public class Dir {
public static void main(String[] args) throws IOException, URISyntaxException {
/**
* 四种构造方法
*/
// 文件绝对路径
testFile1();
// 目录 + 文件
testFile2();
// File对象 + 文件
testFile3();
// URI(不常用)
testFile4();
}
static void testFile1() throws IOException {
// 文件绝对路径
String path = "D:\\JDK\\10";
File file1 = new File(path);
test(file1);
}
static void testFile2() throws IOException {
// 目录
String path = "D:\\JDK\\";
// 文件
String file = "README.html";
File file2 = new File(path, file);
test(file2);
}
static void testFile3() throws IOException {
// 目录
String path = "D:\\JDK";
File file1 = new File(path);
// 文件
String file = "README.html";
File file3 = new File(file1, file);
test(file3);
}
static void testFile4() throws URISyntaxException, IOException {
URI uri = new URI("http://www.baidu.com");
File file4 = new File(uri);
}
public static void test(File file) throws IOException {
boolean newFile = file.createNewFile();
System.out.println(newFile);
}
}
2)、路径组成
测试类
public class Dir {
public static void main(String[] args) throws IOException {
String d1 = "D:\\JDK\\6";
String d2 = "D:\\JDK\\6\\jdk\\README.html";
System.out.println("测试目录:" + d1);
test(new File(d1));
System.out.println("----------------------------------------");
System.out.println("测试文件:" + d2);
test(new File(d2));
}
public static void test(File file) {
// <文件名/最后一级目录名>
String name = file.getName();
// 除了<文件名/最后一级目录名>以外的全路径
String parent = file.getParent();
// <文件名/最后一级目录名>上一级的<文件对象>
File parentFile = file.getParentFile();
// <完整路径>
String path = file.getPath();
System.out.println(name);
System.out.println(parent);
System.out.println(parentFile.getName());
System.out.println(path);
}
}
结果
测试目录:D:\JDK\6
6
D:\JDK
JDK
D:\JDK\6
----------------------------------------
测试文件:D:\JDK\6\jdk\README.html
README.html
D:\JDK\6\jdk
jdk
D:\JDK\6\jdk\README.html
3)、路径操作
测试类
public class Dir {
public static void main(String[] args) throws IOException {
String d1 = "D:\\JDK\\6";
String d2 = "D:\\JDK\\6\\jdk\\README.html";
System.out.println("测试目录:" + d1);
test(new File(d1));
System.out.println("----------------------------------------");
System.out.println("测试文件:" + d2);
test(new File(d2));
}
public static void test(File file) throws IOException {
// 是否为绝对地址
boolean absolute = file.isAbsolute();
// 绝对路径
String absolutePath = file.getAbsolutePath();
// 绝对路径最后一级的<文件对象>
File absoluteFile = file.getAbsoluteFile();
// 绝对路径
String canonicalPath = file.getCanonicalPath();
// 绝对路径最后一级的<文件对象>
File canonicalFile = file.getCanonicalFile();
// URL(不建议使用)
URL url = file.toURL();
// URI
URI uri = file.toURI();
System.out.println(absolute);
System.out.println(absolutePath);
System.out.println(absoluteFile.getName());
System.out.println(canonicalPath);
System.out.println(canonicalFile.getName());
System.out.println(url);
System.out.println(uri);
}
}
结果
测试目录:D:\JDK\6
true
D:\JDK\6
6
D:\JDK\6
6
file:/D:/JDK/6/
file:/D:/JDK/6/
----------------------------------------
测试文件:D:\JDK\6\jdk\README.html
true
D:\JDK\6\jdk\README.html
README.html
D:\JDK\6\jdk\README.html
README.html
file:/D:/JDK/6/jdk/README.html
file:/D:/JDK/6/jdk/README.html
4)、属性访问
测试类
public class Dir {
public static void main(String[] args) throws IOException {
String d1 = "D:\\JDK\\6";
String d2 = "D:\\JDK\\6\\jdk\\README.html";
System.out.println("测试目录:" + d1);
test(new File(d1));
System.out.println("----------------------------------------");
System.out.println("测试文件:" + d2);
test(new File(d2));
}
public static void test(File file){
// 是否存在
boolean exists = file.exists();
// 是否目录
boolean isDirectory = file.isDirectory();
// 是否文件
boolean isFile = file.isFile();
// 是否可读
boolean read = file.canRead();
// 是否可写
boolean write = file.canWrite();
// 是否可执行
boolean execute = file.canExecute();
// 是否隐藏
boolean isHidden = file.isHidden();
// 最后一次修改时间戳
long lastModified = file.lastModified();
// 内容长度
long length = file.length();
System.out.println(exists);
System.out.println(isDirectory);
System.out.println(isFile);
System.out.println(read);
System.out.println(write);
System.out.println(execute);
System.out.println(isHidden);
System.out.println(lastModified);
System.out.println(length);
}
}
结果
测试目录:D:\JDK\6
true
true
true
true
false
false
1500692066520
0
----------------------------------------
测试文件:D:\JDK\6\jdk\README.html
true
true
true
false
true
false
1464564170443
125
5)、文件操作
测试类
public class Dir {
public static void main(String[] args) throws IOException {
String d1 = "D:\\JDK\\10";
String d2 = "D:\\JDK\\10\\jdk\\README.html";
System.out.println("测试目录:" + d1);
test(new File(d1));
System.out.println("----------------------------------------");
System.out.println("测试文件:" + d2);
test(new File(d2));
}
public static void test(File file) throws IOException {
/**
* 列表
*/
// 返回目录下的<文件/目录><名称数组>
String[] list1 = file.list();
// 返回目录下的<文件/目录><名称数组>,筛选文件
String[] list2 = file.list(new FilenameFilter() {
Pattern pattern = Pattern.compile(".*");
public boolean accept(File dir, String name) {
return pattern.matcher(name).matches();
}
});
// 返回目录下的<文件/目录><文件对象数组>
File[] files1 = file.listFiles();
// 返回目录下的<文件/目录><文件对象数组>,筛选文件
File[] files2 = file.listFiles(new FilenameFilter() {
Pattern pattern = Pattern.compile(".*");
public boolean accept(File dir, String name) {
return pattern.matcher(name).matches();
}
});
// 返回目录下的<文件/目录><文件对象数组>,筛选文件
File[] files3 = file.listFiles(new FileFilter() {
Pattern pattern = Pattern.compile(".*");
public boolean accept(File pathname) {
return pattern.matcher(pathname.getName()).matches();
}
});
/**
* 创建
*/
// 创建文件夹(单个)
boolean mkdir = file.mkdir();
// 创建文件夹(多级目录)
boolean mkdirs = file.mkdirs();
// 如果<file对象>不存在,那么创建
boolean newFile = file.createNewFile();
/**
* 删除
*/
// 删除<文件/目录>
boolean delete = file.delete();
// 如果文件存在就删除
file.deleteOnExit();
/**
* 修改
*/
// 同级目录下属于重命名操作、不同目录下属于移动操作
boolean renameTo = file.renameTo(new File("D:\\JDK\\11.txt"));
/**
* 属性
*/
// 设置最后修改<文件/目录>时间戳
boolean lastModified = file.setLastModified(new Date().getTime());
// 设置:只读
boolean readOnly = file.setReadOnly();
// 设置:可写(无论何时所有者可写)
boolean writable1 = file.setWritable(false);
// 设置:可写(设置所有者是否可写)
boolean writable2 = file.setWritable(false, false);
// 设置:可读(无论何时所有者可写)
boolean readable1 = file.setReadable(false);
// 设置:可写(设置所有者是否可写)
boolean readable2 = file.setReadable(false, false);
// 设置:可执行(无论何时所有者可写)
boolean executable1 = file.setExecutable(false);
// 设置:可执行(设置所有者是否可写)
boolean executable2 = file.setExecutable(false, false);
// 测试列表
System.out.println(Arrays.toString(list1));
System.out.println(Arrays.toString(list2));
System.out.println(Arrays.toString(files1));
System.out.println(Arrays.toString(files2));
System.out.println(Arrays.toString(files3));
// 测试创建
System.out.println(mkdir);
System.out.println(mkdirs);
System.out.println(newFile);
// 测试删除
System.out.println(delete);
// 测试修改
System.out.println(renameTo);
// 测试属性设置
System.out.println(lastModified);
System.out.println(readOnly);
System.out.println(writable1);
System.out.println(writable2);
System.out.println(readable1);
System.out.println(readable2);
System.out.println(executable1);
System.out.println(executable2);
}
}
结果
需要分别单独测试,结果略
6)、其他用法
测试类
public class Dir {
public static void main(String[] args) throws IOException {
/**
* 磁盘根目录
*/
File[] files = File.listRoots();
for (int i = 0; i < files.length; i++) {
// 磁盘名
System.out.println(files[i]);
/**
* 磁盘使用情况
*/
// 磁盘总大小
System.out.println(files[i].getTotalSpace());
// 磁盘空闲大小
System.out.println(files[i].getFreeSpace());
// 磁盘可用大小
System.out.println(files[i].getUsableSpace());
}
/**
* 临时目录
*/
// 磁盘临时目录创建文件
File file1 = File.createTempFile("abc", "txt");
// 文件创建后的地址
String absolutePath1 = file1.getAbsolutePath();
System.out.println(absolutePath1);
// ---------------------------------------------------------------------------
// 指定目录创建临时文件
File file2 = File.createTempFile("abc", "txt", new File("D:\\JDK"));
// 文件创建后的地址
String absolutePath2 = file2.getAbsolutePath();
System.out.println(absolutePath2);
}
}
7)、分隔符
测试类
public class Dir {
public static void main(String[] args) throws IOException {
/**
* 路径分隔符
*/
String pathSeparator = File.pathSeparator;
System.out.println(pathSeparator);
char pathSeparatorChar = File.pathSeparatorChar;
System.out.println(pathSeparatorChar);
/**
* 名称分隔符
*/
String separator = File.separator;
System.out.println(separator);
char separatorChar = File.separatorChar;
System.out.println(separatorChar);
}
}
结果
;
;
\
\