IO[File_API學習]

IO流[File_API學習的使用]java

File_API學習的使用
一、名稱分隔符 /  \ separator
java下路徑:\ 在Windows下的路徑,在java裏 \ 是轉義字符。須要 \\
String path = "D:\\JavaCode\\Study_se\\imges\\bug.png"; 
java里路徑表示通常推薦
String path = "D:/JavaCode/Study_se/imges/Dilraba.jpg"; 
常量拼接ide

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

二、構建File對象學習

 1 String path = "D:/JavaCode/Study_se/imges/Dilraba.jpg";
 2 // 一、構造File對象[直接傳入名稱]
 3 File src = new File(path);
 4 System.out.println(src.length());// 打印文件的大小
 5 
 6 // 二、經過父子構建
 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());

三、相對路徑 or 絕對路徑
一、存在盤符:絕對路徑
二、不存在盤符:相對路徑,當前目錄。user.dirspa

 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"));

四、名稱 or 路徑
1.getName():返回名稱
2.path.getPath():返回相對路徑或者絕對路徑
3.getAbsolutePath():返回絕對路徑
4.getParent():返回上一層,父路徑不存在則爲null
5.getParentFile():返回父對象3d

 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());

五、文件的狀態
1.文件是否存在:exists
2.存在
   文件:isFile
   文件夾:isDirectorcode

 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 }

六、其餘信息   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);

七、文件夾的建立_遍歷
* 一、makdir:上級目錄必須存在,不然就建立失敗
* 二、makdirs:上級目錄能夠存在,不存在就先建立上一級【推薦】blog

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

* 三、list():列出下級名稱
* 四、listFile():列出下級File對象
* 五、listRoots():列出全部盤符

八、打印子孫級目錄和文件的名稱
遞歸

 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


九、獲取文件的大小
get

 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
相關文章
相關標籤/搜索