JDK7文件處理

實用的工具類,Path,Paths,Files,FileSystem 

有一些很靈活的處理方法: 

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. //得到一個Path對象  
  2. Path path = Paths.get("/test/a.txt");  
  3. //Path轉換File  
  4. File file = path.toFile();  
  5.   
  6. Files.readAllBytes(path);  
  7. Files.deleteIfExists(path);  
  8. Files.size(path);  

正確拼接路徑不要手動拼接路徑

不好的代碼: 

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. String game = "foo";  
  2. File file = new File("~/test/" + game + ".txt");  
即使是要手動拼接路徑,請使用下面兩個平臺無關的變量: 
[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. System.out.println(File.pathSeparator);  
  2. System.out.println(File.separator);  
正確簡潔的方法是使用Paths類: 
[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. Path path = Paths.get("~/test/""foo""bar""a.txt");  
  2. System.out.println(path);  
  3. //  ~/test/foo/bar/a.txt  

讀取文件的所有內容,文件的所有行

讀取文件所有內容前,先判斷文件大小,防止OOM。 

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public static byte[] readAllBytes(String fileName, long maxSize) throws IOException {  
  2.     Path path = Paths.get(fileName);  
  3.     long size = Files.size(path);  
  4.     if (size > maxSize) {  
  5.         throw new IOException("file: " + path + ", size:" + size + "> " + maxSize);  
  6.     }  
  7.     return Files.readAllBytes(path);  
  8. }  
  9.   
  10. public static List<String> readAlllines(String fileName, Charset charset, long maxSize) throws IOException {  
  11.     Path path = Paths.get(fileName);  
  12.     long size = Files.size(path);  
  13.     if (size > maxSize) {  
  14.         throw new IOException("file: " + path + ", size:" + size + "> " + maxSize);  
  15.     }  
  16.     return Files.readAllLines(path, charset);  
  17. }  

利用JDK7的特性,auto close,遠離一堆的catch, close

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. Path path = Paths.get("~/test/""foo""bar""a.txt");  
  2. try (InputStream in = Files.newInputStream(path)) {  
  3.     // process  
  4.     //in.read();  
  5. }  

歷遍目錄

DK7新特性,FileVisitor 

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public class MyFileVisitor extends SimpleFileVisitor<Path>{  
  2.     @Override  
  3.     public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {  
  4.         System.out.println(file);  
  5.         return FileVisitResult.CONTINUE;  
  6.     }  
  7.       
  8.     public static void main(String[] args) throws IOException {  
  9.         Path path = Paths.get("/home/user/test");  
  10.         Files.walkFileTree(path, new MyFileVisitor());  
  11.     }  
  12. }  

判斷文件是否在父路徑下

網上流傳一種遞歸判斷parent的方式,http://stackoverflow.com/questions/18227634/check-if-file-is-in-subdirectory

但是查閱jdk代碼後,發現getParent()函數是通過處理文件名得到的。所以直接比較文件前綴即可。 

請務必注意,file.getCanonicalPath()函數 

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public static boolean isSubFile(File parent, File child) throws IOException {  
  2.     return child.getCanonicalPath().startsWith(parent.getCanonicalPath());  
  3. }  
  4.   
  5.   
  6. public static boolean isSubFile(String parent, String child) throws IOException {  
  7.     return isSubFile(new File(parent), new File(child));  
  8. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章