0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Javaテストコードメソッド集

Last updated at Posted at 2020-04-12
MyTestUtil.java
package domain.unofficial;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class MyTestUtil {

    private static Path DELETE_PERMISSION_PATH = Paths.get("d:/tmp");

    public static void cat(final String path) throws IOException {
        readAllLines(path).forEach(System.out::println);
    }

    public static void dir(final String path) throws IOException {
        readPathsInfo(path).forEach(System.out::println);
    }

    public static List<String> readAllLines(final String path) throws IOException {
        return Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8);
    }

    public static List<String> readPathsInfo(final String targetPath) throws IOException {
        List<String> result = new ArrayList<>();
        Path target = Paths.get(targetPath);
        result.add(target.toString());
        for (Path p : Files.walk(target).filter(p -> !p.equals(target)).collect(Collectors.toList())) {
            LocalDateTime time = LocalDateTime.ofInstant(Files.getLastModifiedTime(p).toInstant(),
                    ZoneId.systemDefault());

            String dateTime = time.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME).substring(0, 19);
            String attr = null;
            if (Files.isDirectory(p)) {
                attr = String.format("%-10s", "<DIR>");
            } else {
                attr = String.format("%10s", Long.toString(Files.size(p)));
            }
            String relativePath = target.relativize(p).toString();

            result.add(dateTime + "  " + attr + "  " + relativePath);
        }
        return result;
    }

    public static List<String> readAllPaths(final String path) throws IOException {
        return Files.walk(Paths.get(path)).map(Path::toString).collect(Collectors.toList());
    }

    public static void delete(final String targetPath) throws IOException {
        delete(targetPath, false);
    }

    public static void clear(final String targetPath) throws IOException {
        delete(targetPath, true);
    }

    private static void delete(final String targetPath, boolean isClear) throws IOException {
        Path target = Paths.get(targetPath);
        Predicate<Path> filter = null;
        if (isClear) {
            filter = (p) -> {
                return !p.equals(target);
            };
        } else {
            filter = (p) -> true;
        }
        Files.walk(target).sorted(Comparator.reverseOrder()).filter(filter)
                .filter(p -> p.startsWith(DELETE_PERMISSION_PATH)).map(Path::toFile).forEach(File::delete);
    }

    public static void copy(final String sourcePath, final String destinationPath) throws IOException {
        Path source = Paths.get(sourcePath);
        Path destination = Paths.get(destinationPath);

        if (Files.isRegularFile(source) && Files.isDirectory(destination)) {
            destination = destination.resolve(source.getFileName());
        }

        for (final Path sp : Files.walk(source).collect(Collectors.toList())) {
            Files.copy(sp, destination.resolve(source.relativize(sp)), StandardCopyOption.COPY_ATTRIBUTES);
        }
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?