1.學(xué)習(xí)IO的目的就是處理電腦磁盤上的各種文件(文本,音頻,視頻),所有的程序都是加載帶內(nèi)存中進(jìn)行的
而java的IO類可以完成內(nèi)存和磁盤的數(shù)據(jù)交換功能,即通過(guò)io類把磁盤上的文件加載到內(nèi)存中進(jìn)行程序的處理
2.File更應(yīng)該叫做一個(gè)路徑,文件路徑或者文件夾路徑
3.路徑分為絕對(duì)路徑和相對(duì)路徑
4.絕對(duì)路徑是一個(gè)固定的路徑,從盤符開(kāi)始D:\java\2.java基礎(chǔ)\課件視頻\day19\video
5.相對(duì)路徑相對(duì)于某個(gè)位置,在eclipse下是指當(dāng)前項(xiàng)目下,dos在指的當(dāng)前路徑 ? ? 光標(biāo)前的路徑? 就是鼠標(biāo)可以選擇到哪里
?
* File(String pathname):根據(jù)一個(gè)路徑得到File對(duì)象
* File(String parent, String child):根據(jù)一個(gè)目錄和一個(gè)子文件/目錄得到File對(duì)象
* File(File parent, String child):根據(jù)一個(gè)父File對(duì)象和一個(gè)子文件/目錄得到
1 /* 2 * 在實(shí)際開(kāi)發(fā)過(guò)程中,如果用到了絕對(duì)路徑, 應(yīng)該使用下面這種方法 3 * parent表示路徑,child表示文件名 4 * 5 * 根據(jù)一個(gè)父File對(duì)象和一個(gè)子文件/目錄得到File對(duì)象 6 * 封裝成File對(duì)象這樣更好的取使用file類里面的方法。 7 */ 8 String parent = "F:\\基礎(chǔ)課程\\day01\\photo"; 9 String child = "jvm.png";10 File file = new File(parent,child);11 boolean b = file.exists();12 System.out.println(b);13 14 15 //根據(jù)一個(gè)目錄和一個(gè)子文件/目錄得到File對(duì)象16 String parent = "F:\\基礎(chǔ)課程\\day01";17 String child = "photo\\jvm.png";18 File file = new File(parent,child);19 boolean b = file.exists();20 System.out.println(b);
//創(chuàng)建一個(gè)文件對(duì)象
//根據(jù)一個(gè)路徑得到File對(duì)象
File file1 = new File("C:\\a.txt");
?
還有 getAbsolutePath()方法和?getAbsoluteFile()方法
//創(chuàng)建一個(gè)文件對(duì)象
File file1 = new File("C:\\a.txt"); //返回的是文件類型,還可以繼續(xù)使用file類中的各個(gè)方法 File file = file1.getAbsoluteFile(); System.out.println(file);
//輸出結(jié)果為 C:\a.txt
//返回的是字符串類型,可以像字符串一樣進(jìn)行操作 String str = file1.getAbsolutePath(); System.out.println(str);
//輸出結(jié)果為 C:\a.txt
?
?
? createNewFile():創(chuàng)建文件 如果存在這樣的文件,就不創(chuàng)建了
? mkdir():創(chuàng)建文件夾 如果存在這樣的文件夾,就不創(chuàng)建了
mkdirs():創(chuàng)建文件夾,如果父文件夾不存在,會(huì)幫你創(chuàng)建出來(lái)
?
1 File file1 = new File("C:\\a.txt"); 2 //創(chuàng)建指定文件夾,如果沒(méi)有父系文件夾,則會(huì)創(chuàng)建出父文件夾 3 boolean b = file.mkdirs(); 4 System.out.println(b); 5 6 File file1 = new File("C:\\a.txt"); 7 //創(chuàng)建一個(gè)指定文件夾 8 boolean a = file.mkdir(); 9 System.out.println(a);10 11 12 File file1 = new File("C:\\a.txt");13 //創(chuàng)建指定文件14 boolean a = file.createNewFile();15 System.out.println(a);16 17
//(這就是相對(duì)路徑) File file = new File("\\a.txt"); //如果你創(chuàng)建文件或者文件夾忘了寫盤符路徑, //那么,默認(rèn)在項(xiàng)目路徑下。 可以是相對(duì)路徑 boolean b = file.createNewFile();
?
?
來(lái)源:https://www.icode9.com/content-4-402351.html聯(lián)系客服