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. }  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值