package cn.edu.fhj.day009.FileDemo; import java.io.File; import java.io.IOException; public class FileDemo { public static void main(String[] args) throws IOException { // 將路徑描述成File對象 // File file = new File("d:/java_fd_test/fileDemo.txt"); File file = new File( "F:/up_mouth_10/code_data/Data_Structure_And_Algorithm"); boolean exists = file.exists(); // 若是路徑所表示的文件或者文件夾存在,則返回true System.out.println(exists); // 判斷該file是文件夾仍是文件 boolean directory = file.isDirectory(); System.out.println(directory); // true boolean ifFile = file.isFile(); System.out.println(ifFile); // false // 獲取文件的絕對路徑 String absolutePath = file.getAbsolutePath(); System.out.println(absolutePath); // 能夠獲取文件名或文件夾名 String name2 = file.getName(); System.out.println(name2); File file2 = new File( "F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/Demo.txt"); ifFile = file2.isFile(); // true System.out.println(ifFile); // 獲取文件名 String name = file2.getName(); System.out.println(name); // 獲取上一級目錄的file對象 File parentFile = file2.getParentFile(); System.out.println(parentFile.getAbsolutePath()); // 獲取上一級目錄的路徑字符串 String parent = file2.getParent(); System.out.println(parent); // 獲取文件長度 字節(8個bit-- 二進制位) long length = file2.length(); System.out.println(length); System.out.println("------------------------"); // 獲取指定目錄下的子節點的名稱字符串 String[] list = file.list(); for (String s : list) { System.out.println(s); } System.out.println("------------------------"); // 獲取指定目錄下的子節點的File描述對象 File[] listFiles = file.listFiles(); for (File f : listFiles) { System.out.println(f.getAbsolutePath()); } System.out.println("------------------------"); // 建立一個文件夾 File f = new File( "F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/xx/yy/zz"); // boolean mkdir = f.mkdir(); // 不能建立多級目錄 // System.out.println(mkdir); // boolean mkdirs = f.mkdirs(); // 能夠建立多級目錄 // System.out.println(mkdirs); // // // 建立文件 File file3 = new File( "F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/xx/yy/zz/cls.txt"); boolean createNewFile = file3.createNewFile(); System.out.println(createNewFile); // 重命名文件:其實能夠把路徑都給改了 file3.renameTo(new File( "F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/xx/yy/zz/cls001.txt")); // 刪除文件 boolean delete = file3.delete(); System.out.println(delete); } }
package cn.edu.fhj.day009.FileDemo; import java.io.FileOutputStream; public class FileOutputStreamDemo { public static void main(String[] args) throws Exception { // 覆蓋的方式寫數據 FileOutputStream fos = new FileOutputStream( "F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/Demo.txt"); String s = "a你好"; byte[] bytes = s.getBytes(); fos.write(bytes); // 將字符串按指定編碼集編碼--》將信息轉成二進制數 fos.write(bytes); // 這樣寫入的數據,會將文件中的原數據覆蓋 // 追加的方式寫數據:若是要往一個文件中追加數據,則在FileOutputStream的構造參數中多傳一個true FileOutputStream fos2 = new FileOutputStream( "F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/Demo.txt", true); fos2.write(",sb".getBytes("UTF-8")); fos2.close(); /** * 第一句和後兩句話寫到文件中的數據徹底相同 */ fos.write("我用一輩子一世爲你祈禱".getBytes()); // .getBytes()編碼的過程 fos.write((byte) 49); fos.write((byte) 51); /** * 這兩句話寫到文件中的數據徹底相同 */ // fos.write((byte)13); // fos.write("\r".getBytes()); fos.close(); } }
package cn.edu.fhj.day009.FileDemo; import java.io.FileInputStream; import java.io.InputStream; public class FileInputStreamDemo { public static void main(String[] args) throws Exception { // 要讀文件,首先要構造一個FileInputStream對象 InputStream fis = new FileInputStream( "F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/Demo.txt"); /** * 把數從文件中讀取出來 如何讀取字符 */ // FileInputStream是一種字節流,是按照一個一個字節去文件中取數據的 // 手動一個字節一個字節地讀取 /* * int read = fis.read(); * * System.out.println(read); * * read = fis.read(); System.out.println(read); */ /** * 利用fis讀到文件末尾後會返回-1的特性,來用循環進行讀取 */ int read = 0; /* * while((read=fis.read())!=-1) { System.out.println(read); } */ System.out.println("-------------------"); /** * 若是我要讀出數據(文本文件中的數據其實就是字符) 過程是:仍是先讀數,而後按照碼錶,將這個數轉成字符 * */ /* * read = 0; while((read=fis.read())!=-1) { // * char就表明一個英文字符,並且使用的是ascII碼錶規則 char c = (char)read; * System.out.println(c); } */ /** * 一次讀取多個字節而後轉成某種數據類型 read(buf)方法,一次讀取buf長度個字節數據,而且讀到的數據直接填入了buf數組中 */ /* * byte[] buf = new byte[8]; int num = fis.read(buf); // 返回的是真實讀到的字節數量 * String string = new String(buf,2,5); // 利用二進制的byte數組來轉成字符串 * System.out.println(string); */ /** * 用while循環來反覆讀取 */ int num = 0; byte[] buf = new byte[8]; while ((num = fis.read(buf)) != -1) { System.out.println(new String(buf, 0, num)); } // 關流 fis.close(); } }