1. 程式人生 > >IO[File_API學習]

IO[File_API學習]

hide view 常量 tro length ima 文件夾 png 調用

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:\JavaCode\Study_se\imges\Dilraba.jpg
 6 File src = new File("Dilraba.jpg");
 7 System.out.println("相對路徑:" + src.getPath());// 相對路徑:Dilraba.jpg
 8 System.out.println("返回絕對路徑:" + path.getAbsolutePath());// 返回絕對路徑:D:\JavaCode\Study_se\imges\Dilraba.jpg
 9 System.out.println("返回父路徑:" + path.getParent());// 返回父路徑:D:\JavaCode\Study_se\imges
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

IO[File_API學習]