目錄
- 1 file類分隔符
- 2 建立file對象
- 3 File類中的各種常用方法
- 3.1 getName()擷取執行個體對象代表的檔案名字(包含檔案字尾)
- 3.2 getParent() 擷取執行個體對象代表的檔案上級目錄
- 3.3 getParentFile()
- 3.4 getPath() 檔案的實際路徑
- 3.5 isAbsolute() 是否是絕對路徑
- 3.6 getAbsolutePath()檔案的絕對路徑
- 3.7 exists() 執行個體對象代表的檔案是否存在
- 3.8 isFile() 檢測執行個體對象代表的是否是檔案
- 3.9 isDirectory() 是否是目錄
- 3.10 createNewFile() 建立新檔案
- 3.11 delete() 删除執行個體對象代表的檔案或目錄
- 3.12 mkdir()
- 3.13 mkdirs() 建立目錄
- 3.14 list()
- 3.15 listFiles() 過濾資料
1 file類分隔符
public static void main(String[] args) {
System.out.println(File.pathSeparator );// Linux為冒号 :Windows為分号 ;
System.out.println(File.pathSeparatorChar );// Linux為冒号 :Windows為分号 ;
System.out.println(File.separator );// Linux為正斜杠 / Windows為反斜杠 \
System.out.println(File.separatorChar );// Linux為正斜杠 / Windows為反斜杠 \
}
複制
2 建立file對象
注意:構造函數隻是建立一個File執行個體,并沒有以檔案做讀取等操作,是以路徑即使是錯誤的,也可以建立執行個體不報錯
- 2.1) 若子路徑child為Null,會抛出NullPointerException空異常錯誤
- 2.2) 當父路徑為Null時,會以子路徑child作為絕對路徑建立執行個體,等同于調用第一個File(String child )效果一樣
3 File類中的各種常用方法
3.1 getName()擷取執行個體對象代表的檔案名字(包含檔案字尾)
File file = new File("D:\\資料\\test.txt");
File file3 = new File("xxx"); // 錯誤路徑
System.out.println(file.getName()); // 結果是 test.txt
System.out.println(file3.getName()); // 結果是 xxx
複制
3.2 getParent() 擷取執行個體對象代表的檔案上級目錄
public class Test2
{
public static void main(String[] args) throws URISyntaxException
{
File file = new File("E:/test/1.txt");
File file2 = new File("E:/test/異常1.jpg");
File file3 = new File("xxx"); // 錯誤路徑
System.out.println(file.getParent()); // 結果是E:\test
System.out.println(file2.getParent()); // 結果是E:\test
System.out.println(file3.getParent()); // 結果是null
}
}
複制
3.3 getParentFile()
擷取執行個體對象的父項的執行個體對象,如果此路徑名未指定父目錄,則傳回null;也就是擷取對象的上級目錄然後再執行個體化一個對象;
public class Test2
{
public static void main(String[] args) throws URISyntaxException
{
File file = new File("E:/test/1.txt");
File file2 = new File("E:/test/異常1.jpg");
File file3 = new File("xxx"); // 錯誤路徑
System.out.println(file.getParentFile()); // 結果是E:\test
System.out.println(file2.getParentFile()); // 結果是E:\test
System.out.println(file3.getParentFile()); // 結果是null
}
}
複制
3.4 getPath() 檔案的實際路徑
public class Test2
{
public static void main(String[] args) throws URISyntaxException
{
File file = new File("E:/test/1.txt");
File file2 = new File("E:/test/異常1.jpg");
File file3 = new File("xxx"); // 錯誤路徑
System.out.println(file.getPath()); // 結果是E:\test\1.txt
System.out.println(file2.getPath()); // 結果是E:\test\異常1.jpg
System.out.println(file3.getPath()); // 結果是xxx
}
}
複制
3.5 isAbsolute() 是否是絕對路徑
檢測該執行個體對象代表的檔案的路徑是否是絕對路徑(windows系統中路徑是以驅動盤開始的就是絕對路徑)
public class Test2
{
public static void main(String[] args) throws URISyntaxException
{
File file = new File("E:/test/1.txt");
File file2 = new File("E:/test/異常1.jpg");
File file3 = new File("xxx"); // 錯誤路徑
System.out.println(file.isAbsolute()); // 結果是true
System.out.println(file2.isAbsolute()); // 結果是true
System.out.println(file3.isAbsolute()); // 結果是false
}
}
複制
3.6 getAbsolutePath()檔案的絕對路徑
public class Test2
{
public static void main(String[] args) throws URISyntaxException
{
File file = new File("E:/test/1.txt");
File file2 = new File("E:/test/異常1.jpg");
File file3 = new File("xxx"); // 錯誤路徑
System.out.println(file.getAbsolutePath()); // 結果是E:\test\1.txt
System.out.println(file2.getAbsolutePath()); // 結果是E:\test\異常1.jpg
System.out.println(file3.getAbsolutePath()); // 結果是D:\workspace\lettcode\xxx(編譯器的工作空間作為解析目錄了)
}
}
複制
3.7 exists() 執行個體對象代表的檔案是否存在
public class Test2
{
public static void main(String[] args) throws URISyntaxException
{
File file = new File("E:/test/1.txt");
File file2 = new File("E:/test/異常1.jpg");
File file3 = new File("xxx"); // 錯誤路徑
System.out.println(file.exists()); // 結果是true
System.out.println(file2.exists()); // 結果是true
System.out.println(file3.exists()); // 結果是false
}
}
複制
3.8 isFile() 檢測執行個體對象代表的是否是檔案
public class Test2
{
public static void main(String[] args) throws URISyntaxException
{
File file = new File("E:/test/1.txt");
File file2 = new File("E:/test/異常1.jpg");
File file3 = new File("xxx"); // 錯誤路徑
System.out.println(file.isFile()); // 結果是true
System.out.println(file2.isFile()); // 結果是true
System.out.println(file3.isFile()); // 結果是false
}
}
複制
3.9 isDirectory() 是否是目錄
public class Test2
{
public static void main(String[] args) throws URISyntaxException
{
File file = new File("E:/test/1.txt");
File file2 = new File("E:/test/異常1.jpg");
File file3 = new File("xxx"); // 錯誤路徑
System.out.println(file.isDirectory()); // 結果是false
System.out.println(file2.isDirectory()); // 結果是false
System.out.println(file3.isDirectory()); // 結果是false
}
}
複制
3.10 createNewFile() 建立新檔案
建立新檔案–當且僅當執行個體對象代表的檔案不存在時才可以建立新檔案
public class Test2
{
public static void main(String[] args) throws IOException
{
File file = new File("E:/test/1.txt");
File file2 = new File("E:/test/異常1.jpg");
File file3 = new File("xxx"); // 錯誤路徑
System.out.println(file.createNewFile()); // 結果是false,已經存在無法建立
System.out.println(file2.createNewFile()); // 結果是false,已經存在無法建立
System.out.println(file3.createNewFile()); // 結果是true,不存在可以建立
}
}
複制
3.11 delete() 删除執行個體對象代表的檔案或目錄
删除執行個體對象代表的檔案或目錄,當代表目錄時,必須目錄下為空才可以删除
public class Test2
{
public static void main(String[] args) throws IOException
{
File file = new File("E:/test/");
File file2 = new File("E:/test/異常1.jpg");
File file3 = new File("xxx"); // 錯誤路徑
System.out.println(file.delete()); // 結果是false,目錄無法删除
System.out.println(file2.delete()); // 結果是true,檔案可以删除
System.out.println(file3.delete()); // 結果是true,檔案可以删除
}
}
複制
3.12 mkdir()
根據執行個體對象的路徑名建立目錄(若目錄已存在,則false;若路徑是檔案,則fasle;若路徑的上級目錄不存在則false)
public class Test2
{
public static void main(String[] args) throws IOException
{
File file = new File("E:/test1/");
File file2 = new File("E:/test2/異常1.jpg");
File file3 = new File("E:/ceshi");
File file4 = new File("E:/ceshi2/2018");
System.out.println(file.mkdir()); // false 因為test1目錄已存在
System.out.println(file2.mkdir()); // false,因為上級目錄test2不存在
System.out.println(file3.mkdir()); // true,因為ceshi目錄不存在且是上級目錄是E盤存在的
System.out.println(file4.mkdir()); // false,因為ceshi2目錄不存在,是以不成功
}
}
複制
3.13 mkdirs() 建立目錄
根據執行個體對象的路徑建立目錄,包括建立那些必須的且不存在的父級目錄
public class Test2
{
public static void main(String[] args) throws IOException
{
File file = new File("E:/test1/");
File file2 = new File("E:/test2/異常1.jpg");
File file3 = new File("E:/ceshi");
File file4 = new File("E:/ceshi2/zhagnsan");
System.out.println(file.mkdirs()); // false 因為test1目錄已存在
System.out.println(file2.mkdirs()); // true,因為目錄不存在是以可以建立,但注意生成的異常1.jpg是檔案夾不是圖檔
System.out.println(file3.mkdirs()); // true,因為目錄不存在是以可以建立
System.out.println(file4.mkdirs()); // true,因為目錄不存在是以可以建立
}
}
複制
3.14 list()
擷取執行個體對象代表的檔案下的各級檔案名和目錄名,傳回一個字元串數組 ;
1. 當執行個體對象代表的是檔案不是目錄時,傳回NUll對象
2. 擷取的是該目錄下的檔案名和目錄名,并不包含該目錄名稱和其上級目錄名稱
3. 字元串數組中都是檔案名或目錄名并不是路徑名
4. 字元串中的元素并不是按照實際系統中目錄下的順序排列的
複制
3.15 listFiles() 過濾資料
我們有一個目錄,想要擷取目錄下的特定的檔案,那麼就可以使用這個,自己定義過濾規則;
這個方法的參數是一個接口,并且這個接口裡面隻有一個方法
那麼,我們參數裡面可以使用lamabda表達式進行表示
File file = new File("D:\\資料");
File[] files = file.listFiles(pathname -> pathname.toString().endsWith(".txt"));
if(files != null && files.length != 0){
for(File item:files){
System.out.println(item.getPath());
}
}
複制
結果
D:\資料\test.txt
D:\資料\Z.txt
複制