// 判断目录或者文件是否可操作
public boolean canExecute()
api: Tests whether the application can execute the file denoted by this abstract pathname. On some platforms it may be possible to start the Java virtual machine with special privileges that allow it to execute files that are not marked executable. Consequently this method may return true even though the file does not have execute permissions.
// 判断目录或者文件是否可读
public boolean canRead()
api: Tests whether the application can read the file denoted by this abstract pathname. On some platforms it may be possible to start the Java virtual machine with special privileges that allow it to read files that are marked as unreadable. Consequently this method may return true even though the file does not have read permissions.
// 判断目录或者文件是否可写
public boolean canWrite()
Tests whether the application can modify the file denoted by this abstract pathname. On some platforms it may be possible to start the Java virtual machine with special privileges that allow it to modify files that are marked read-only. Consequently this method may return true even though the file is marked read-only.
E:\Test
File child = new File("E:\\Test\\aa");
// exists()
// 判断child 文件或目录是否存在
boolean canExecute = child.exists();
System.out.println(canExecute);
结果:false
// compareTo(File pathname)
// 比较两个 file对象的路径
File a = new File("E:\\Te");
File b = new File("E:\\Test");
int canExecute = a.compareTo(b);
System.out.println(canExecute);
int canExecute = b.compareTo(a);
System.out.println(canExecute);
结果: -2 2
// createNewFile()
// 创建一个新文件,文件名不能重复;如 A文件夹下有个 AA文件或者目录,新创建的文件名字不能为 AA
try {
File a = new File("E:\\Test\\aa");
boolean canExecute = a.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
结果
// 创建临时文件 使用 prefix作为文件的前缀 + 系统随机的一段数字字符 + suffix作为文件的后缀
// directory为空,使用系统默认的临时文件夹
createTempFile(String prefix, String suffix )
createTempFile(String prefix, String suffix, File directory)
try {
File.createTempFile("aaaaaa",".wodecsdn",new File("E:\\Test"));
File.createTempFile("aaa",".wodecsdn",new File("E:\\Test"));
File.createTempFile("aaaaaa","wodecsdn",new File("E:\\Test"));
} catch (IOException e) {
e.printStackTrace();
}
结果
// delete() deleteOnExit()
// 都能删除文件,不同之处在于delete 删除立即生效,deleteOnExit 删除是在虚拟机退出时。
// api:public void deleteOnExit()
Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates. Files (or directories) are deleted in the reverse order that they are registered. Invoking this method to delete a file or directory that is already registered for deletion has no effect. Deletion will be attempted only for normal termination of the virtual machine, as defined by the Java Language Specification.
Once deletion has been requested, it is not possible to cancel the request. This method should therefore be used with care.
Note: this method should not be used for file-locking, as the resulting protocol cannot be made to work reliably. The FileLock facility should be used instead.
// getAbsoluteFile()
// 返回抽象路径a 代表的文件或文件夹的绝对路径
// getAbsolutePath()
// 返回抽象路径a 代表的文件或文件夹的绝对路径,以字符串的形式
File a = new File("Test");
File absoluteFile = a.getAbsoluteFile();
String absolutePath = a.getAbsolutePath();
System.out.println(absoluteFile);
System.out.println(absoluteFile.toString());
System.out.println(absoluteFile.toPath());
System.out.println(absolutePath);
结果:
// getCanonicalFile() 和 getCanonicalPath()的关系
与 getAbsoluteFile() 和 getAbsolutePath()的关系 类似
// getCanonicalFile 得到给规范化的路径
ps:getCanonicalFile 和 getCanonicalPath 会抛出一个 IOException 异常
File a = new File("..\\Test");
File absoluteFile = null;
File absolutePath = null;
try {
absoluteFile = a.getAbsoluteFile();
absolutePath = a.getCanonicalFile();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(absoluteFile);
System.out.println(absolutePath);
结果:
// getFreeSpace() 返回未分配的空间大小
File a = new File("E:\\Test");
long absoluteFile = a.getFreeSpace();
System.out.println(absoluteFile);
结果:
看看下面这两张图
// getName() 返回文件或文件夹的名字,以抽象路径的形式
File a = new File("E:\\Test");
String absoluteFile = a.getName();
System.out.println(absoluteFile);
//
结果:
// getParent()
// 返回抽象路径的父目录,以字符串的形式
// getParentFile()
// 返回抽象路径的父目录,以文件或目录的形式
File a = new File("E:\\");
File b = new File("E:\\Test\\aa\\bb");
File c = new File("bb");
File d = new File("\\Test\\aa\\bb");
String absoluteFile = a.getParent();
System.out.println(absoluteFile);
absoluteFile = b.getParent();
System.out.println(absoluteFile);
absoluteFile = c.getParent();
System.out.println(absoluteFile);
absoluteFile = d.getParent();
System.out.println(absoluteFile);
结果:
// getPath()
// 将一个抽象路径转换为路径,以字符串的形式返回
File a = new File("E:\\");
File b = new File("E:\\Test\\aa\\bb");
File c = new File("bb");
File d = new File("\\Test\\aa\\bb");
String absoluteFile = a.getPath();
System.out.println(absoluteFile);
absoluteFile = b.getPath();
System.out.println(absoluteFile);
absoluteFile = c.getPath();
System.out.println(absoluteFile);
absoluteFile = d.getPath();
System.out.println(absoluteFile);
结果:
// getTotalSpace()
// 返回分区磁盘大小;
// ps:抽象路径不存在,返回0
File a = new File("E:\\");
File b = new File("E:\\Test");
File c = new File("E:\\Test\\bb");
long absoluteFile = a.getTotalSpace();
System.out.println(absoluteFile);
absoluteFile = b.getTotalSpace();
System.out.println(absoluteFile);
absoluteFile = c.getTotalSpace();
System.out.println(absoluteFile);
结果:
看看下面几张图
// getUsableSpace() 和 getFreeSpace()类似,但是比getFreeSpace()更准确
// 返回可用的空间大小
File a = new File("E:\\");
File b = new File("E:\\Test");
File c = new File("E:\\Test\\bb");
long absoluteFile = a.getUsableSpace();
System.out.println(absoluteFile);
absoluteFile = b.getUsableSpace();
System.out.println(absoluteFile);
absoluteFile = c.getUsableSpace();
System.out.println(absoluteFile);
结果: