// File常用的屬性和方法public static void fileTest() throws IOException {System.out.println("系統有關的路徑分隔字元:" + File.pathSeparator);System.out.println("系統有關的路徑分隔字元:" + File.pathSeparatorChar);System.out.println("與系統有關的預設名稱分隔字元:" + File.separator);System.out.println("與系統有關的預設名稱分隔字元:" + File.separatorChar);File file = new File("H:\\2T100\\1.txt");System.out.println("1.txt檔案是否存在:" + file.exists());// 如果檔案不存在,建立新檔案if (!file.exists()) {file.createNewFile();}file = new File("H:\\2T100", "1.jpg");System.out.println("1.jpg是否可讀:" + file.canRead());System.out.println("1.jpg是否可寫:" + file.canWrite());System.out.println("絕對路徑 :" + file.getAbsolutePath());System.out.println("相對路徑 :" + file.getPath());System.out.println("檔案名稱:" + file.getName());System.out.println("路徑:" + file.getParent());System.out.println("是否是檔案:" + file.isFile());System.out.println("是否是目錄:" + file.isDirectory());System.out.println("檔案大小:" + file.length() + "位元組");}// 列印指定檔案或目錄包括所有子項目public static void printFileList(String path, int level) {// 構建一個File執行個體File f = new File(path);StringBuilder sb = new StringBuilder();// 對目錄下的子項目進行縮排,顯示層次感for (int i = 0; i < level; i++) {sb.append("\t");}// 如果檔案不為空白if (f != null) {// 如果是目錄if (f.isDirectory()) {// 輸出目錄名System.out.println(sb.toString() + f.getName());// 獲得目錄的子項目的集合File[] fileArray = f.listFiles();List<File> fileList = sortFile(fileArray);if (fileArray != null) {// 如果目錄不為空白,迴圈得到子項目for (int i = 0; i < fileList.size(); i++) {printFileList(fileArray[i].getPath(), level + 1); // 遞迴調用}}// 如果是檔案,直接輸出檔案名} else {System.out.println(sb.toString() + f.getName());}}}// 排序,檔案在前,目錄在後public static List<File> sortFile(File[] files) {LinkedList<File> list = new LinkedList<File>();for (File file : files) {if (file.isFile()) {//如果是檔案,就放在最前面list.addFirst(file);} else {//如果是目錄,就放在最後面list.addLast(file);}}return list;}// 獲得指定目錄裡指定尾碼名的檔案public static void printFileName(String path) {File f = new File(path);if (f.isDirectory()) {//獲得子項目的集合File[] list = f.listFiles(new JavaFileFilter());//遍曆集合,輸出檔案名for (File file : list) {System.out.println(file.getName());}}}// 靜態內部類static class JavaFileFilter implements FilenameFilter {@Overridepublic boolean accept(File dir, String name) {//檢測以.java為尾碼的檔案if (name.endsWith(".java")) {return true;}return false;}}// 位元組流讀寫public static void outputStreamTest() {File f = new File("d:" + File.separator + "test.txt");// 檔案存在,進行操作if (f.exists()) {byte[] b = new byte[1024]; // 位元組緩衝區int len = -1; // 讀取到的位元組的個數InputStream is = null;OutputStream os = null;try {// 構造輸入資料流is = new BufferedInputStream(new FileInputStream(f));// 構造輸出資料流os = new BufferedOutputStream(new FileOutputStream(new File("d:" + File.separator + "test1.txt")));// 讀到-1時表示檔案結束while ((len = is.read(b)) != -1) {// 讀到len位元組就寫len個位元組os.write(b);System.out.println("寫入" + len + "個位元組");}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {//重新整理輸出資料流並強制寫出所有緩衝的輸出位元組os.flush();} catch (IOException e) {e.printStackTrace();}try {//關閉輸出資料流os.close();} catch (IOException e) {e.printStackTrace();}try {//關閉輸入資料流is.close();} catch (IOException e) {e.printStackTrace();}}}}//字元流讀寫public static void rwTest(){int len=-1;char[] c=new char[1024]; // 字元緩衝區Reader reader=null;Writer writer=null;try {// 構造輸入資料流reader=new FileReader(new File("d:"+File.separator+"test.txt"));// 構造輸出資料流writer=new FileWriter("d:"+File.separator+"1.txt");// 讀到-1時表示檔案結束while((len=reader.read(c))!=-1){// 讀到len字元就寫len個字元writer.write(c,0,len);System.out.println("寫入" + len + "個字元");}} catch (IOException e) {e.printStackTrace();}finally{try {//重新整理輸出資料流並強制寫出所有緩衝的輸出字元writer.flush();} catch (IOException e) {e.printStackTrace();}try {//關閉輸出資料流writer.close();} catch (IOException e) {e.printStackTrace();}try {//關閉輸入資料流reader.close();} catch (IOException e) {e.printStackTrace();}}}//字元流整行整行讀寫public static void bufferedRWTest(){File f=new File("d:"+File.separator+"test.txt");String temp=null; //字串緩衝區BufferedReader br=null;BufferedWriter bw=null;try {// 構造輸入資料流br=new BufferedReader(new FileReader(f));// 構造輸出資料流bw=new BufferedWriter(new FileWriter("d:"+File.separator+"123.txt"));// 讀到null時表示檔案結束while((temp=br.readLine())!=null){// 讀到多少行就寫多少行bw.write(temp);//寫入一個行分隔字元bw.newLine();}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e){e.printStackTrace();} finally {try {//重新整理輸出資料流並強制寫出所有緩衝的輸出字元bw.flush();} catch (IOException e) {e.printStackTrace();}try {//關閉輸出資料流bw.close();} catch (IOException e) {e.printStackTrace();}try {//關閉輸入資料流br.close();} catch (IOException e) {e.printStackTrace();}}}