一、File类的一些常用方法:
1.createNewFile
public boolean createNewFile()
throws IOException
Atomically creates a new, empty file named by this abstract pathname if and only if a file with
this name does not yet exist. The check for the existence of the file and the creation of the
file if it does not exist are a single operation that is atomic with respect to all other
filesystem activities that might affect the file.
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.
Returns:
true if the named file does not exist and was successfully created; false if the named file
already exists
Throws:
IOException - If an I/O error occurred
SecurityException - If a security manager exists and its SecurityManager.checkWrite
(java.lang.String) method denies write access to the file
Since:
1.2
2.delete
public boolean delete()
Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a
directory, then the directory must be empty in order to be deleted.
Returns:
true if and only if the file or directory is successfully deleted; false otherwise
3.renameTo
public boolean renameTo(File dest)
Renames the file denoted by this abstract pathname.
Many aspects of the behavior of this method are inherently platform-dependent: The rename
operation might not be able to move a file from one filesystem to another, it might not be
atomic, and it might not succeed if a file with the destination abstract pathname already
exists. The return value should always be checked to make sure that the rename operation was
successful.
Parameters:
dest - The new abstract pathname for the named file
Returns:
true if and only if the renaming succeeded; false otherwise
这个方法可以用来移动文件夹的同时,修改文件夹的名称(同样适用于文件)
二、几个容易混淆的方法
(1)getPath()与getAbsolutePath()的区别
public static void test1(){
File file1 = new File(".\\test1.txt");
File file2 = new File("D:\\workspace\\test\\test1.txt");
System.out.println("-----默认相对路径:取得路径不同------");
System.out.println(file1.getPath());
System.out.println(file1.getAbsolutePath());
System.out.println("-----默认绝对路径:取得路径相同------");
System.out.println(file2.getPath());
System.out.println(file2.getAbsolutePath());
}
得到的结果:
-----默认相对路径:取得路径不同------
.\test1.txt
D:\workspace\test\.\test1.txt
-----默认绝对路径:取得路径相同------
D:\workspace\test\test1.txt
D:\workspace\test\test1.txt
因为getPath()得到的是构造file的时候的路径。
getAbsolutePath()得到的是全路径
如果构造的时候就是全路径那直接返回全路径
如果构造的时候试相对路径,返回当前目录的路径+构造file时候的路径
(2).getAbsolutePath()和getCanonicalPath()的不同
public static void test2() throws Exception{
File file = new File("..\\src\\test1.txt");
System.out.println(file.getAbsolutePath());
System.out.println(file.getCanonicalPath());
}
得到的结果
D:\workspace\test\..\src\test1.txt
D:\workspace\src\test1.txt
可以看到CanonicalPath不但是全路径,而且把..或者.这样的符号解析出来。
(3).
public static void test3() throws Exception{
File file = new File("D:\\Text.txt");
System.out.println(file.getCanonicalPath());
}
确定你的系统是Windows系统。
确定D盘下没有Text.txt这个文件,直接执行这段代码,得到的结果是:
D:\Text.txt
注意这里试大写的Text.txt
在D盘下建立一个文件,名叫text.txt,再次执行代码,得到结果
D:\text.txt
同样的代码得到不同的结果。
同时可以对比getAbsolutePath()看看,这个得到的结果是一样的。
原因:
window是大小写不敏感的,也就是说在windows上test.txt和Test.txt是一个文件,所以在windows上当文件不
存在时,得到的路径就是按照输入的路径。但当文件存在时,就会按照实际的情况来显示。这也就是建立文件
后和删除文件后会有不同的原因。文件夹和文件类似。