Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ua.nure.bekuzarov.Task5;
- import java.io.File;
- import java.io.IOException;
- import java.util.Date;
- public class JDir {
- private static boolean printInfo, addSeparator, lastTimeModify, printSize;
- private static final String r = "-r", f = "-f", d = "-d", s = "-s";
- public static void usage() {
- System.out.println("Usage: JDir [options] [FSE]");
- }
- public static void main(String[] args) throws IOException {
- if (args == null) {
- usage();
- return;
- }
- paramsParser(args);
- printInfo = false;
- addSeparator = false;
- lastTimeModify = false;
- printSize = false;
- }
- public static void printInfo(File file, String ext) {
- if (ext != null && file.isFile() && !file.getName().endsWith(ext))
- return;
- if (ext != null) {
- if (file.isFile())
- System.out.println(getInformation(file));
- } else {
- System.out.println(getInformation(file));
- }
- File[] files = file.listFiles();
- if (files == null)
- return;
- for (File f : files) {
- printInfo(f, ext);
- }
- }
- public static void printInfo(File file) {
- if (!printInfo && !addSeparator && !printSize && !lastTimeModify)
- printInfo = true;
- String name = file.getName();
- if (name.contains("*.")) {
- String canonical = file.getAbsolutePath();
- String[] s = canonical.split("\\*");
- printInfo(new File(s[0]), s[1]);
- } else
- printInfo(file, null);
- }
- private static void paramsParser(String[] args) throws IOException {
- boolean userDir = true;
- for (String arg : args) {
- if (checkSettings(arg))
- continue;
- userDir = false;
- printInfo(new File(arg));
- }
- if (userDir)
- printInfo(new File(System.getProperty("user.dir")));
- }
- public static boolean checkSettings(String option) {
- if (option == r) {
- printInfo = true;
- return true;
- }
- if (option == f) {
- addSeparator = true;
- return true;
- }
- if (option == d) {
- lastTimeModify = true;
- return true;
- }
- if (option == s) {
- printSize = true;
- return true;
- }
- return false;
- }
- private static String getInformation(File file) {
- StringBuilder sb = new StringBuilder();
- if (printInfo)
- sb.append(file.getAbsolutePath());
- if (addSeparator)
- if (!file.isFile())
- sb.append(file.getAbsolutePath() + File.separator);
- if (lastTimeModify)
- sb.append(" \nLast modification time: "
- + (new Date(file.lastModified())));
- if (printSize)
- sb.append(" \nByte Size: " + file.length() + " bytes");
- sb.append(System.lineSeparator());
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement