File类常用方法介绍

Java文件File类

File类

1 概念

java.io.File类:

文件和目录路径名的抽象表示(代表硬盘上的一个文件或者文件夹)

主要用于文件和目录的创建、查找和删除等操作。

1.1 相对路径与绝对路径

相对路径:相对于项目目录的路径,这是一个便捷的路径,开发中经常使用。

绝对路径:从盘符开始的完整路径

1.2 路径的表示方式

java中文件路径的表示方式

Windows中表示: c:\suns.txt

Java中表示: c:\\suns.txt

   c:/sun.txt

2 构造方法

 1  public static void main(String[] args) {
 2    //方式1:根据路径名创建File对象
 3    File f=new File("D:/Practice/day01/lina.text");
 4    //方式2:
 5    String parent="d:/Practice/day01/";//父路径
 6    String child="lina.text";//子路径
 7    File f2=new File(parent, child);
 8    //方式3:
 9    File parent1=new File("d:/Practice/day01/");//对象类型
10    File f3=new File(parent1, child);
11    System.out.println(f);
12    System.out.println(f2);
13    System.out.println(f3);
14  }

3 常用方法

3.1创建功能的方法

 1  public static void main(String[] args) {
 2    File f=new File("d:/Practice/day01/lina.txt");//不带后缀名也是空文档
 3    File f2=new File("d:/Practice/day01/myfile/aa");
 4    //创建目录(文件夹)
 5    boolean res1=f2.mkdir();//创建单极的目录,已经存在创建失败
 6    System.out.println(res1);
 7    res1=f2.mkdirs();//创建多级的目录,已经存在创建失败
 8    System.out.println(res1);
 9    try {
10      //创建空文件:当前仅当当该文件不存在的时候创建成功,如果 已经存在创建失败。
11     boolean res=f.createNewFile();
12      System.out.println("创建file " +res);
13    } catch (IOException e) {
14      e.printStackTrace();
15    }
16  }
3.2删除功能的方法

 1  public static void main(String[] args) {
 2    File f = new File("d:/lina.txt");
 3    File f2 = new File("D:\\myfile2\\music\\china");
 4    // 创建目录(文件夹)
 5    //boolean res = f2.mkdir();//创建单级目录
 6    boolean res = f2.mkdirs();//创建多级目录
 7    res=f2.delete();
 8    System.out.println("删除f2:"+res);
 9    //删除文件或者目录:delete :只能删除存在的文件或(非空)目录
10    res=f.delete();
11    System.out.println("删除文件:"+res);
12    System.out.println(res);
13  }
3.3判定功能的方法

 1  public static void validate(File file) {
 2    boolean res=file.isDirectory();//判定创建是否是一个目录
 3    boolean res1=file.isFile();//判定是否是一个文件
 4    if(!res1) {
 5      System.out.println("file是一个目录");
 6    }else {
 7      System.out.println("file是一个文件");
 8    }
 9  }
10  public static void main(String[] args) {
11    File f= new File("d:/Practice/day01/lina.txt");
12    File f2=new File("d:/Practice/day01/myfile/aa");
13    //判定文件或者目录是否存在
14    boolean res =f.exists();
15    System.out.println("f是否存在:"+res);
16    validate(f);
17    res =f2.exists();
18    System.out.println("f是否存在:"+res);
19    validate(f2);
20  }
3.4获取功能的方法

 1 public static void fun(File file) {
 2         //获取该文件的名称
 3         String name=file.getName();
 4         System.out.println("文件名称:"+name);
 5         //获取文件的路径
 6         String path=file.getAbsolutePath();
 7         System.out.println("文件的路径是:"+path);
 8         //获取文件的长度:如果file是文件,长度就是指文件的大小(字节) 如果file是个目录,返回值就不确定了
 9         long len=file.length();
10         if(file.isFile()) {//file是文件,len获取的就是文件的大小
11             System.out.println("文件的长度是:"+len);
12         }else {
13             System.out.println("目录的长度是返回值不确定:"+len);
14         }
15     }
16     public static void main(String[] args) {
17         File file=new File("lina.text");//相对路径
18         //文件是否存在
19         if(!file.exists()) {
20             if(file.isFile()) {
21                 //创建文件
22                 try {
23                     boolean res=file.createNewFile();//创建成功后文件会在项目的根路径下
24                     if(res) {
25                         System.out.println("新建文件成功!");
26                     }else {
27                         System.out.println("创建文件失败!");
28                     }
29                 } catch (IOException e) {
30                     
31                     e.printStackTrace();
32                 }
33             }else {
34                 //创建目录
35                 boolean res=file.mkdirs();
36                 if(res) {
37                     System.out.println("新建目录成功!");
38                 }else {
39                     System.out.println("创建目录失败!");
40                 }
41             }
42         }
43         //获取文件信息
44         fun(file);
45     }
3.5目录的遍历

 1 public static void fun(File file) {
 2         // 获取该目录下的所有的文件名称
 3         String[] arr=file.list();
 4         File[] arr2=file.listFiles();
 5         if(arr!=null) {
 6             for (String string : arr) {
 7                 System.out.println(string);
 8             }
 9         }else {
10             System.out.println("不是目录或者目录中没有内容");
11         }
12     }
13     public static void main(String[] args) {
14         File file = new File("d:/");
15         System.out.println("输出"+file.getName()+"目录下的所有文件内容:");
16         fun(file);
17     }

4 综合练习

遍历目录中的所有文件

 1 public static void main(String[] args) {
 2         File file = new File("C:/Program Files");
 3         pringFileName(file);
 4     }
 5     public static void pringFileName(File file) {
 6         if (file != null) {
 7             // 获取指定目录中的所有的File
 8             File[] fs = file.listFiles();
 9             if (fs != null) {
10                 for (File f : fs) {
11                     if (f != null && f.isFile()) {// 是文件的时候直接打印文件名称
12                         System.out.println(f.getName());
13                     } else {// 是文件夹(目录)---继续打印目录中的内容
14                         pringFileName(f);
15                     }
16                 }
17             }
18         }
19     }

File类中的方法综合练习

 1 public static void fun(File file) throws IOException {
 2         // 判定文件是否存在
 3         if (!file.exists()) {
 4             // 判定创建的是目录还是文件夹
 5             if (file.isDirectory()) {// 是否是一个目录 file.isFile()
 6                 // 是目录--调用目录的创建方法
 7                 boolean res = file.mkdirs();
 8                 if (res) {
 9                     System.out.println("创建" + file.getName() + "目录成功!绝对路径是:" + file.getAbsolutePath());
10                 } else {
11                     System.out.println("创建失败!");
12                 }
13             } else {
14                 // 是文件--调用创建文件的方法
15                 boolean res = file.createNewFile();
16                 if (res) {
17                     System.out.println("创建" + file.getName() + "文件成功!绝对路径是:" + file.getAbsolutePath());
18                 } else {
19                     System.out.println("创建失败!");
20                 }
21             }
22         }else {
23             System.out.println("你创建的文件或目录存在!!");
24         }
25     }
26     public static void main(String[] args) {
27         File file = new File("lina.txt");
28         try {
29             fun(file);
30         } catch (IOException e) {
31             e.printStackTrace();
32         }
33     }
posted @ 2020-06-16 00:29  不被替代的坚持  阅读(360)  评论(0编辑  收藏  举报