java IO流技術 之 File,javaio

來源:互聯網
上載者:User

java IO流技術 之 File,javaio

IO流技術

  概念:input - output 輸入輸出資料流:

  輸入:將檔案讀到記憶體中;

  輸出:將檔案從記憶體中寫出到其他地方

  作用:主要就是解決裝置和裝置之間的資料轉送問題。

File :檔案類的使用十分重要

(一)file的構造方法

 1     public static void test1(){ 2          3         //三種拼接路徑方式 4         //1.直接拼接 5         File filePath = new File("C:\\123\\aaa"); 6         System.out.println("是否存在檔案夾--"+filePath.exists()); 7          8         //2.先拼接根路徑,在組合子路徑 9         File parentPath = new File("C:\\123\\aaa");10         File childPath = new File(parentPath,"bb.txt");11         System.out.println("是否存在檔案夾--"+childPath.exists());12     13         //3.根路徑和子路徑使用逗號隔開14         File filePath2 = new File("C:\\123\\aaa","bb.txt");15         System.out.println("是否存在檔案--"+filePath2.exists());16                 17     }

 ----------------------------------

(二)基本方法

 1 public static void test2() throws IOException{ 2          3         //1,.getAbsolutePath()  擷取當的本地路徑 4         File file = new File(""); 5         System.out.println("當前java工程的本地絕對路徑:"+file.getAbsolutePath()); 6         System.out.println("是否存在檔案夾--"+file.exists()); 7          8          9         //2,.mkdir() 建立檔案夾10         File dirCreatePath = new File("E:\\abc");11         System.out.println("檔案夾是否建立成功--"+dirCreatePath.mkdir());12         13         File dirCreatePaths = new File("E:\\abc\\ddd");14         System.out.println("檔案夾是否建立成功--"+dirCreatePaths.mkdirs());15         16         //3,.createNewFile(); 指定路徑建立一個檔案,17         File fileCreatePath = new File("E:\\abc\\123.txt");18         File fileCreatePath = new File("E:\\abc\\456.txt");19         System.out.println("是否建立成功"+fileCreatePath.createNewFile());20         21         //4,oldFileNamePath.renameTo(fileNewNamePath)  重新命名22         //(1)方式一23         File fileName = new File("E:\\abc\\456.txt");24         if(fileName.exists()){25             File fileNewName = new File("E:\\abc\\666.txt");26             System.out.println("重新命名是否成功--"+fileName.renameTo(fileNewName));27         }else{28             System.out.println("重新命名失敗!");29         }30         31         //(2)方式二32         File oldFile = new File("E:\\abc\\666.txt");33         System.out.println("重新命名是否成功--"+oldFile.renameTo(new File("E:\\abc\\888.txt")));34     35         //(3),更換隱藏檔夾(或者儲存盤)36         File oldFile = new File("E:\\abc\\888.txt");37         System.out.println("更換隱藏檔夾是否成功--"+oldFile.renameTo(new File("E:\\abc\\ddd\\888.txt")));38         39     40         //5,(1)delete(); 刪除檔案或者 空 的檔案夾41         File fileDelete = new File("E:\\abc\\123.txt");42         System.out.println("是否成功刪除--"+fileDelete.delete());43         44         //(2)deleteOnExit();  在jvm退出時候刪除45         File fileDeleteOnExit = new File("E:\\abc\\ddd\\888.txt");46         System.out.println("是否成功刪除--"+fileDeleteOnExit.deleteOnExit());47     48         for(int i = 0;i<=10000;i++){49             if(i==10000){ //jvm退出後再執行 deleteOnExit()50                 System.out.println("jvm 已退出!");51             }52         }53     54     }

 ----------------------------------

(三)判斷方法

 1     //判斷方法 2 //    isFile()        判斷是不是檔案(是,則返回 true) 3 //    isDirectory()   判斷是不是檔案夾(是,則返回true) 4 //    isHidden()      判斷是不是隱藏檔案或檔案夾(是,則返回true) 5 //    isAbsolute()    判斷是不是絕對路徑(是,返回true) 6      7     public static void test3(){ 8         File file = new File("E:\\abc\\ggg\\hhh.txt");//檔案中 9         File file2 = new File("E:\\abc\\ggg");10         if(file2.exists()){11             System.out.println("是檔案?"+file.isFile());12             System.out.println("2是檔案夾?"+file2.isDirectory());13             System.out.println("2是隱藏檔案?"+file2.isHidden());14             System.out.println("是絕對路徑?"+file.isAbsolute());15             System.out.println("2是絕對路徑?"+file2.isAbsolute());16         }17     }

 ----------------------------------

(四)擷取方法

 1     //擷取方法 2 //    getName()            擷取檔案名稱字(路徑最後分隔字元後面的名稱) 3 //    getPath()            擷取路徑 4 //    length()             擷取檔案容量大小 5 //    getParent()          擷取檔案或檔案夾的父類路徑 6 //    getAbsolutePath()    擷取絕對路徑 7 //    lastModified()       擷取檔案最後修改的時間戳記(毫秒) 8      9     public static void test4(){10         File file = new File("E:\\abc\\ggg\\hhh.txt");11         if(file.exists()){12             System.out.println("檔案名稱--"+file.getName());13             System.out.println("檔案路徑--"+file.getPath());14             System.out.println("檔案絕對路徑--"+file.getAbsolutePath());15             System.out.println("檔案大小--"+file.length());16             System.out.println("檔案的父類目錄--"+file.getParent());17             System.out.println("檔案路徑最後修改時間(毫秒)--"+file.lastModified());18             19             //將毫秒轉為日期20             long time = file.lastModified();21             Date date = new Date(time);22             SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");23             System.out.println("檔案路徑最後修改時間(毫秒)--"+dateFormat.format(date));24             25         }else {26             System.out.println("不存在");27         }28         29     }

 -----------------------------------

(五)對檔案的操作

 1     //檔案夾的操作 2     static int sumByte = 0; 3     public static void test5(){ 4          5         //【1】擷取盤符,返回數組(不需要new) 6         File[] fileRoots = File.listRoots(); 7         for(File fileRoot : fileRoots){ 8             System.out.println("原生磁碟:"+fileRoot); 9         }10         11         //【2】擷取檔案夾的所有檔案數組12         //(1)建立路徑13         File filePath = new File("E:\\IT學習大綱");14         15         //(2)將路徑裡的檔案及檔案夾放到檔案類數組中16         File[] files = filePath.listFiles();17         18         System.out.println("IT學習大綱:");19         //(3)將檔案數組遍曆出來20         for(File file1Child : files){21             22             //(4)判斷為檔案夾則直接列印,如果是檔案則列印名字並計算容量大小23             if(file1Child.isDirectory()){24                 System.out.println("\n--"+file1Child.getName());25                 26                 //(5)依次嵌套遍曆。【也可以使用遞迴調用的方法來一層一層的遍曆出下一級的檔案或檔案夾】27                 File[] file2 = file1Child.listFiles();28                 for(File file2Child : file2){29                     if(file2Child.isDirectory()){30                         31                         System.out.println("-- --"+file2Child.getName());32                         33                         //(6)34                         File[] file3 = file2Child.listFiles();35                         for(File file3Child : file3){36                             if(file3Child.isDirectory()){37                                 System.out.println("-- -- --"+file3Child.getName());38                                 39                             }else{40                                 System.out.println("-- -- -->"+file3Child.getName()+"\t"+file3Child.length()+"bt");41                                 sumByte += file3Child.length();42                             }43                             44                         }45                     }else{46                         System.out.println("-- -->"+file2Child.getName()+"\t"+file2Child.length()+"bt");47                         sumByte += file2Child.length();48                     }49                     50                 }51                 52             }else{53                 System.out.println("-->"+file1Child.getName()+"\t"+file1Child.length()+"bt");54                 sumByte += file1Child.length();55             }56         }57         58         System.out.println("檔案的總容為:"+sumByte);59         60     }61     62     63     //【3】尋找檔案 需要定義一個介面類64     public static void test6(){65         File file = new File("E:\\IT學習大綱");66         System.out.println(file.exists());67         String[] strings = file.list(new MyFilter());68         69         for(String string : strings){70             System.out.println(string);71         }72     }73 74     //尋找檔案的過濾器75 //    class MyFilter implements FilenameFilter{76 //77 //        @Override78 //        public boolean accept(File dir, String name) {79 //            // TODO Auto-generated method stub80 //            81 //            //設定過濾思路  (這裡比較尾碼名Java)82 //            //(1)方法一83 //            //name = name.substring(name.lastIndexOf("."));84 //            //return ".java".equals(name);85 //    86 //            //(2)方法二87 //            //return name.contains("java");88 //    89 //            //(3)方法三90 //            return name.endsWith("java");91 //        }92 //    }93     

陌陌說:

  資料是永久儲存到本地,資料儲存到磁碟上是以檔案的形式儲存的

  關於目錄分格符:

    Windows系統上的目錄分隔字元:“\”

    Linux系統上的分隔字元:“/”

    注意:   在windows上“\”和“/”都可以作為目錄分隔字元,

        如果使用的是“\”,目錄分隔字元就是“\\”;

        如果你使用了“/”,那麼分隔字元就直接是“/”

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.