Java snippets - nimrody/knowledgebase GitHub Wiki

Java snippets

  • Removes a directory and all of its subdirectories and their files.

    /**

    • Removes a directory and all of its subdirectories and their files.

    • @param path The path of an existing directory. Returns siletly if the path does not exist.

    • @throws IOException If failed to traverse tree (due to permissions, hardware failure, etc.) */ static void removeDir(Path path) throws IOException { if (!path.toFile().exists()) { return; } Files.walkFileTree(path, new SimpleFileVisitor() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; }

       @Override
       public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
           Files.delete(dir);
           return FileVisitResult.CONTINUE;
       }
      

      });

  • Refer to subclass in base class

    class A<T extends A> { T foo(); } class B extends A { @Override B foo(); }

⚠️ **GitHub.com Fallback** ⚠️