IO[File_API学习]

IO流[File_API学习的使用]

File_API学习的使用
1、名称分隔符 /  separator
java下路径: 在Windows下的路径,在java里 是转义字符。需要 \
String path = "D:\JavaCode\Study_se\imges\bug.png"; 
java里路径表示一般推荐
String path = "D:/JavaCode/Study_se/imges/Dilraba.jpg"; 
常量拼接

path = "D:" + File.separator + "JavaCode" + File.separator + "Study_se" + File.separator + "src"
                + File.separator + "imges" + File.separator + "bug.png";

2、构建File对象

 1 String path = "D:/JavaCode/Study_se/imges/Dilraba.jpg";
 2 // 1、构造File对象[直接传入名称]
 3 File src = new File(path);
 4 System.out.println(src.length());// 打印文件的大小
 5 
 6 // 2、通过父子构建
 7 src = new File("D:/JavaCode/Study_se/imges","Dilraba.jpg");
 8 System.out.println(src.length());// 打印文件的大小
 9 
10 // 父对象子名称
11 src = new File(new File("D:/JavaCode/Study_se/imges"),"Dilraba.jpg");
12 System.out.println(src.length());

3、相对路径 or 绝对路径
1、存在盘符:绝对路径
2、不存在盘符:相对路径,当前目录。user.dir

 1 String path = "D:/JavaCode/Study_se/imges/Dilraba.jpg";
 2 
 3 // 绝对路径
 4 File src = new File(path);
 5 // 获得绝对路径getAbsolutePath
 6 System.out.println(src.getAbsolutePath());
 7 
 8 // 相对路径
 9 src = new File("Dilraba.jpg");
10 System.out.println(src.getAbsolutePath());
11 
12 // 用户的目录,当前的工程
13 System.out.println(System.getProperty("user.dir"));

4、名称 or 路径
1.getName():返回名称
2.path.getPath():返回相对路径或者绝对路径
3.getAbsolutePath():返回绝对路径
4.getParent():返回上一层,父路径不存在则为null
5.getParentFile():返回父对象

 1 // 基本信息
 2 File path = new File("D:/JavaCode/Study_se/imges/Dilraba.jpg");
 3 System.out.println("返回名称:" + path.getName());// 返回名称:Dilraba.jpg
 4 // path.getPath() 相对或者绝对
 5 System.out.println("返回路径:" + path.getPath());// 返回路径:D:JavaCodeStudy_seimgesDilraba.jpg
 6 File src = new File("Dilraba.jpg");
 7 System.out.println("相对路径:" + src.getPath());// 相对路径:Dilraba.jpg
 8 System.out.println("返回绝对路径:" + path.getAbsolutePath());// 返回绝对路径:D:JavaCodeStudy_seimgesDilraba.jpg
 9 System.out.println("返回父路径:" + path.getParent());// 返回父路径:D:JavaCodeStudy_seimges
10 // 父路径不存在,则返回null
11 System.out.println(src.getParent());// null
12 System.out.println(path.getParentFile().getName());

5、文件的状态
1.文件是否存在:exists
2.存在
   文件:isFile
   文件夹:isDirector

 1 // 文件状态
 2 src = new File("xxx");
 3 if(src == null || !src.exists()) {
 4     System.out.println("文件不存在");
 5 } else {
 6     if(src.isFile()) {
 7         System.out.println("文件操作");
 8     } else {
 9         System.out.println("文件夹操作");
10     }
11 }

6、其他信息   length():返回一个文件的字节数   不存在创建 ,存在就返回true :createNewFile();【异常抛出去】 删除已存在文件:delete()

1 File file = new File("D:/JavaCode/Study_se/imges/Dilraba.jpg");
2 System.out.println("返回文件的长度:" + file.length());// 返回文件的长度:35004
3 
4 file = new File("D:/JavaCode/Study_se/imges");
5 System.out.println("文件夹:" + file.length());// 文件夹:0
6 
7 file = new File("D:/JavaCode/Study_se/a.txt");
8 boolean flag = file.createNewFile();
9 System.out.println(flag);

7、文件夹的创建_遍历
* 1、makdir:上级目录必须存在,否则就创建失败
* 2、makdirs:上级目录可以存在,不存在就先创建上一级【推荐】

File dir = new File("D:/JavaCode/Study_se/dir/test");
// 创建目录
boolean flag = dir.mkdirs();
System.out.println(flag);

* 3、list():列出下级名称
* 4、listFile():列出下级File对象
* 5、listRoots():列出所有盘符

8、打印子孙级目录和文件的名称

 1 package boom.io;
 2 
 3 import java.io.File;
 4 
 5 public class DirDeme4 {
 6 /**
 7  * 递归:方法自己调用自己
 8  * @param args
 9  */
10     public static void main(String[] args) {
11         File src = new File("D:/BaiduPCS-Go");
12         printName(src,0);
13     }
14     // 打印子孙级目录和文件的名称
15     public static void printName(File src,int deep){
16         // 控制前面层次
17         for(int i=0;i<deep;i++){
18             System.out.print("-");
19         }
20         // 打印名称
21         System.out.println(src.getName());
22         if(src == null || !src.exists()){// 递归头
23             return;
24         }else if (src.isDirectory()){// 是否是目录
25             for(File s : src.listFiles()){
26                 printName(s,deep+1);// 递归体
27             }
28         }
29         
30     }
31 }
View Code


9、获取文件的大小

 1 package boom.io;
 2 
 3 import java.io.File;
 4 
 5 public class DirDeme5 {
 6 /**
 7  * 递归:统计文件夹的大小
 8  * @param args
 9  */
10     public static void main(String[] args) {
11         File src = new File("D:/BaiduPCS-Go");
12         count(src);
13         System.out.println(len);
14     }
15     private static long len = 0;
16     public static void count(File src){
17         // 获取文件的大小
18         if(src != null && src.exists()){
19             if (src.isFile()) {// 大小
20                 len += src.length();
21             } else {// 子孙级
22                 for (File s : src.listFiles()) {
23                     count(s);
24                 }
25             }
26         }
27     }
28 
29 }
View Code
原文地址:https://www.cnblogs.com/cao-yin/p/9650093.html