zoukankan      html  css  js  c++  java
  • java 文件相关问题

    1.文件路径

       分隔符

     1 System.out.println(File.separator);
     2 System.out.println(File.pathSeparator);
     3 
     4 /*mac os 和 linux 下的执行结果*/
     5 /
     6 :
     7 
     8 /*windows 下的执行结果*/
     9 
    10 ;

       文件路径尽量直接使用字符串拼接,不建议的写法如下

    String fileName = "test";
    File file = new File("/szc/log/"+fileName+".log");
    

      尤其对于windows系统,文件分隔符 “” ,在java中需要转义,所以windows的文件路径写法如下,但是这种写法复杂且易错。

    String fileName = "test";
    File file = new File("d:\szc\log\"+fileName+".log");

      如果必须使用字符串拼接路径,建议使用平台无关的分隔符

    File linFile = new File(File.separator+"szc"+File.separator+"log"+File.separator+"test.log");
    File winFile = new File("D:"+File.separator+"szc"+File.separator+"log"+File.separator+"test.log"); 

       但是这种写法需要大量使用File.separator,jdk7 提供了更为简洁和优雅方式,但是请注意一定要求jdk7以上

    Path winPath = Paths.get("d:", "szc","log","test.log");
    Path linPath = Paths.get("/szc","log","test.log");

    2. JDK7 auto close 原理一堆catch  close

    新增了try-with-resource 异常声明

    在JDK7中只要实现了AutoCloseableCloseable接口的类或接口,都可以使用try-with-resource来实现异常处理和资源关闭

    异常抛出顺序。

    在Java se 7中的try-with-resource机制中异常的抛出顺序与Java se 7以前的版本有一点不一样,是先声明的资源后关闭

    JDK7以前如果rd.readLine()与rd.close()(在finally块中)都抛出异常则只会抛出finally块中的异常,不会抛出rd.readLine();中的异常。这样经常会导致得到的异常信息不是调用程序想要得到的。

    JDK7及以后版本中如果采用try-with-resource机制,如果在try-with-resource声明中抛出异常(可能是文件无法打或都文件无法关闭)同时rd.readLine();也抛出异常,则只会抛出rd.readLine()的异常

    实例代码如下:

     //JDK1.7以前的版本,释放资源的写法
     static String readFirstLingFromFile(String path) throws IOException {
      BufferedReader br = null;
      try {
       br = new BufferedReader(new FileReader(path));
       return br.readLine();
      } catch (IOException e) {
       e.printStackTrace();
      } finally {
       if (br != null)
        br.close();
      }
      return null;
     }
     //JDK1.7中的写法,利用AutoCloseable接口
     //代码更精练、完全
     static String readFirstLineFromFile(String path) throws IOException {
      try (BufferedReader br = new BufferedReader(new FileReader(path))) {
       return br.readLine();
      }
     }
    }

    但是,注意JDK7 以后,在try-with-resource 后一定要抛出异常 throws IOException,否在无法通过编译

    3.实用的工具类,Path,Paths,Files,FileSystem 

    //得到一个Path对象
    Path path = Paths.get("/test/a.txt");
    //Path转换File
    File file = path.toFile();
            
    Files.readAllBytes(path);
    Files.deleteIfExists(path);
    Files.size(path);

    4. FileVistor 遍历目录和文件

        FileVisitor是一个接口,Files.walkFileTree是一个方法。 通过两者的配合可以遍历整个某个路径下的所有子路径和文件。没有这个之前我们用递归方法也能实现,有了这个不能说是现实更加容易, 只能说是现实更加规范, 如果大家都用这个,代码的可维护性会更好。我觉得仅此而已。 

      FileVisitor有四个方法 

    FileVisitResult postVisitDirectory(T dir, IOException exc)
        Invoked for a directory after entries in the directory, and all of their descendants, have been visited.
    FileVisitResult preVisitDirectory(T dir, BasicFileAttributes attrs)
        Invoked for a directory before entries in the directory are visited.
    FileVisitResult visitFile(T file, BasicFileAttributes attrs)
        Invoked for a file in a directory.
    FileVisitResult visitFileFailed(T file, IOException exc)
        Invoked for a file that could not be visited.

    preVisitDirectory - 一个路径被访问时调用

    PostVisitDirectory - 一个路径的所有节点被访问后调用。如果有错误发生,exception会传递给这个方法

    visitFile - 文件被访问时被调用。该文件的文件属性被传递给这个方法

    visitFileFailed - 当文件不能被访问时,此方法被调用。Exception被传递给这个方法。

    如果你比较懒,不想实现所有方法。你可以选择继承 SimpleFileVisitor。它帮你实现了上述方法,你只需Override 你感兴趣的方法。

    FileVisitResult枚举类型: 

    CONTINUE,SKIP_SIBLINGS,SKIP_SUBTREE,TERMINATE
    CONTINUE –继续
    TERMINATE –终止,这次遍历结束了
    SKIP_SUBTREE –子树(当前路径的子目录)不再遍历了
    SKIP_SIBLINGS –兄弟节点(同级别目录)不再访问了。
  • 相关阅读:
    Jessica's Reading Problem POJ
    FatMouse and Cheese HDU
    How many ways HDU
    Humble Numbers HDU
    Doing Homework again
    Stacks of Flapjacks UVA
    Party Games UVA
    24. 两两交换链表中的节点
    面试题 03.04. 化栈为队
    999. 可以被一步捕获的棋子数
  • 原文地址:https://www.cnblogs.com/SamuelSun/p/5464546.html
Copyright © 2011-2022 走看看