目录
列:exists判断文件或文件夹是否存在、isDirectory判断是否是目录、isFile判断是否是文件
列:createNewFile创建文件、delete删除文件或目录、mkdir创建目录、mkdirs创建多级目录
列:目录(文件夹)遍历 :list():返回字符串、listFiles():返回目录
File抽象类
判断目录与文件是否存在的方法
public boolean exists() :此File表示的文件或目录是否实际存在。
public boolean isDirectory() :此File表示的是否为目录。
public boolean isFile() :此File表示的是否为文件。
列:exists判断文件或文件夹是否存在、isDirectory判断是否是目录、isFile判断是否是文件
public class Demo04File {
public static void main(String[] args) {
show1();
show2();
}
// public boolean exists() :判断文件或文件夹是否存在
private static void show1() {
File f1 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan");
System.out.println(f1.exists());//true
File f2 = new File("C:\\Users\\itcast\\IdeaProjects\\shung");
System.out.println(f2.exists());//false
File f3 = new File("shungyuan.iml");//相对路径 C:\Users\itcast\IdeaProjects\shungyuan\shungyuan.iml
System.out.println(f3.exists());//true
File f4 = new File("a.txt");//判断目录是否存在
System.out.println(f4.exists());//true
}
/*2 public boolean isDirectory() :此File表示的是否为目录。
public boolean isFile() :此File表示的是否为文件。
*/
private static void show2() {
File f1 = new File("C:\\Users\\itcast\\IdeaProjects\\shung");
//不存在,就没有必要获取
if(f1.exists()){
System.out.println(f1.isDirectory());
System.out.println(f1.isFile());
}
Fil