1 public class Demo1_File { 2 3 /* 4 * File(String pathname) 根據一個路徑獲得File對象 5 * File(String parent, String child) 根據一個目錄和一個子文件/目錄獲得File對象 6 * File(File parent, String child) 根據一個父類File對象和一個子類文件/目錄獲得File對象 7 */ 8 public static void main(String[] args) { 9 //demo1(); 10 //demo2(); 11 //將父級目錄封裝成一個對象是由於這樣能夠操做文件類的一些方法 12 String parent = "d:\\test"; 13 String child = "test.txt"; 14 File file = new File(parent); 15 File file2 = new File(file, child); 16 System.out.println(file2.exists()); 17 18 } 19 20 /** 21 * 把父級目錄和子級目錄分開能夠使得靈活性更高,更容易修改對應目錄 22 */ 23 public static void demo2() { 24 String parent = "d:\\test"; 25 String child = "test.txt"; 26 File file = new File(parent, child); 27 System.out.println(file.exists()); 28 } 29 30 public static void demo1() { 31 File file = new File("d:\\test.txt"); 32 System.out.println(file.exists()); 33 } 34 35 }
1 public class Demo2_File { 2 3 /* 4 * 建立功能: 5 * public boolean createNewFile() 建立文件,若是文件存在,就再也不建立返回false 6 * public boolean mkdir() 建立文件夾,若是存在就再也不建立返回false 7 * public boolean mkdirs() 建立多級目錄,若是父級目錄不存在則會幫忙建立出來 8 * 注意: 若是不加盤符,則會默認爲當前項目目錄 9 * 10 * 重命名功能: 11 * public boolean renameTo(File dest)把文件重命名爲指定的文件路徑 12 * public boolean delete() 刪除文件或文件夾 13 * 重命名注意事項: 14 * 若是路徑名相同則只是更名,若是路徑名不一樣,則是更名並剪切 15 * 刪除功能注意事項: 16 * Java中的刪除不走回收站; 17 * 要刪除一個文件夾時,文件夾裏面不能包含文件或文件夾,不然會刪除失敗 18 * 19 * 判斷功能: 20 * public boolean isDirectory() 判斷是不是目錄 21 * public boolean isFile() 判斷是不是文件 22 * public boolean exists() 判斷文件是否存在 23 * 24 * public boolean canRead() 判斷文件是否可讀 25 * public boolean canWrite() 判斷是否可寫 26 * public boolean isHidden() 判斷是否隱藏 27 * 28 * 獲取功能: 29 * public String getAbsolutePath() 獲取絕對路徑 30 * public String getPath() 獲取構造方法中傳入的路徑 31 * public String getName() 獲取文件或文件夾名稱 32 * public long length() 獲取長度,字節數 33 * public long lastModified() 獲取最後一次的修改時間,毫秒值 34 * public Sting[] list() 獲取指定目錄下的全部文件或者文件夾的名稱數組(僅僅獲取文件名稱) 35 * public File[] listFiles() 獲取指定目錄下的全部文件或文件夾的File數據(帶文件絕對路徑的) 36 * 37 */ 38 /** 39 * @param args 40 * @throws IOException 41 */ 42 public static void main(String[] args) throws IOException { 43 44 //demo1(); 45 //demo2(); 46 //demo3(); 47 } 48 49 /** 50 * 判斷功能 51 */ 52 public static void demo3() { 53 File file = new File("aa"); 54 file.setReadable(false); //windows系統中認爲全部的文件都是可讀的 55 file.setWritable(false); //windows系統中能夠設置爲不可寫 56 } 57 58 /** 59 * 重命名及刪除功能 60 */ 61 public static void demo2() { 62 File file = new File("ccc"); 63 System.out.println(file.renameTo(new File("aaaa"))); 64 File file1 = new File("aaaa"); 65 System.out.println(file1.renameTo(new File("d:\\ccc"))); //剪切並重命名 66 67 File file2 = new File("aaa.txt"); 68 System.out.println(file2.delete()); 69 File file3 = new File("aaa"); 70 System.out.println(file3.delete()); //刪除不成功,由於文件夾aaa裏面有文件夾 71 } 72 73 /** 74 * @throws IOException 75 * 建立功能 76 */ 77 public static void demo1() throws IOException { 78 File file = new File("ccc"); 79 System.out.println(file.createNewFile()); 80 81 File file2= new File("aaa.txt"); //這樣寫也是能夠的,目錄名也能夠有後綴名 82 System.out.println(file2.mkdir()); 83 84 File file3 = new File("aaa\\ccc"); 85 System.out.println(file3.mkdirs()); 86 } 87 88 }