All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dotty.tools.scaladoc.util.IO Maven / Gradle / Ivy

There is a newer version: 3.6.0-RC1-bin-20240917-6e852d2-NIGHTLY
Show newest version
package dotty.tools.scaladoc.util;

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.function.Consumer;
import java.nio.charset.Charset;

/** This code is mostly using public snippets and tries to mimic sbt-io api. */
public class IO {
  public static void delete(File pathToBeDeleted) throws IOException {
    Files.walkFileTree(pathToBeDeleted.toPath(),
      new SimpleFileVisitor() {
        @Override
        public FileVisitResult postVisitDirectory(
          Path dir, IOException exc) throws IOException {
            Files.delete(dir);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(
          Path file, BasicFileAttributes attrs)
          throws IOException {
            Files.delete(file);
            return FileVisitResult.CONTINUE;
        }
    });
  }

  public static void foreachFileIn(Path dir, Consumer op) throws IOException {
    Files.walkFileTree(dir, new SimpleFileVisitor() {
        @Override
        public FileVisitResult visitFile(
          Path file, BasicFileAttributes attrs)
          throws IOException {
            op.accept(file);
            return FileVisitResult.CONTINUE;
        }
    });
  }

  public static String read(Path path) throws IOException {
    return new String(Files.readAllBytes(path), Charset.defaultCharset());
  }

  public static String read(String path) throws IOException {
    return read(Paths.get(path));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy