-
-
Save toby55kij/625ad401c13dd2ca644e5fd0f90f550c to your computer and use it in GitHub Desktop.
JBangを利用したサンプルプログラム(フォルダ内にある全てのファイルについてバイト数を表示)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///usr/bin/env jbang "$0" "$@" ; exit $? | |
//DEPS info.picocli:picocli:4.6.3 | |
//JAVA 11+ | |
import picocli.CommandLine; | |
import picocli.CommandLine.Command; | |
import picocli.CommandLine.Parameters; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.util.concurrent.Callable; | |
import java.util.stream.Stream; | |
@Command(name = "PrintSize", mixinStandardHelpOptions = true, version = "PrintSize 0.1", | |
description = "Print file size") | |
class PrintSize implements Callable<Integer> { | |
@Parameters(index = "0", description = "file path", defaultValue = ".") | |
private Path path; | |
@CommandLine.Option(names = {"-d", "--delimiter"}, defaultValue = "\t", | |
description = "delimiter (default: \\t)") | |
private String delimiter; | |
public static void main(String... args) { | |
int exitCode = new CommandLine(new PrintSize()).execute(args); | |
System.exit(exitCode); | |
} | |
@Override | |
public Integer call() { | |
printFileSize(path.normalize().toAbsolutePath()); | |
return 0; | |
} | |
private void printFileSize(final Path path) { | |
try { | |
if (Files.isDirectory(path)) { | |
printFolders(path); | |
} else { | |
System.out.printf("%s%s%d%n", path, delimiter, Files.size(path)); | |
} | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
private void printFolders(final Path path) throws IOException { | |
try (Stream<Path> children = Files.list(path)) { | |
children.forEachOrdered(this::printFileSize); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment