Advertisement
Konnor95

Untitled

May 29th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. package ua.nure.bekuzarov.Task5;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.Date;
  6.  
  7. public class JDir {
  8.  
  9.     private static boolean printInfo, addSeparator, lastTimeModify, printSize;
  10.     private static final String r = "-r", f = "-f", d = "-d", s = "-s";
  11.  
  12.     public static void usage() {
  13.         System.out.println("Usage: JDir [options] [FSE]");
  14.     }
  15.  
  16.     public static void main(String[] args) throws IOException {
  17.         if (args == null) {
  18.             usage();
  19.             return;
  20.         }
  21.         paramsParser(args);
  22.         printInfo = false;
  23.         addSeparator = false;
  24.         lastTimeModify = false;
  25.         printSize = false;
  26.     }
  27.  
  28.     public static void printInfo(File file, String ext) {
  29.         if (ext != null && file.isFile() && !file.getName().endsWith(ext))
  30.             return;
  31.         if (ext != null) {
  32.             if (file.isFile())
  33.                 System.out.println(getInformation(file));
  34.         } else {
  35.             System.out.println(getInformation(file));
  36.         }
  37.         File[] files = file.listFiles();
  38.         if (files == null)
  39.             return;
  40.         for (File f : files) {
  41.             printInfo(f, ext);
  42.         }
  43.     }
  44.  
  45.     public static void printInfo(File file) {
  46.         if (!printInfo && !addSeparator && !printSize && !lastTimeModify)
  47.             printInfo = true;
  48.         String name = file.getName();
  49.         if (name.contains("*.")) {
  50.             String canonical = file.getAbsolutePath();
  51.             String[] s = canonical.split("\\*");
  52.             printInfo(new File(s[0]), s[1]);
  53.         } else
  54.             printInfo(file, null);
  55.  
  56.     }
  57.  
  58.     private static void paramsParser(String[] args) throws IOException {
  59.         boolean userDir = true;
  60.         for (String arg : args) {
  61.             if (checkSettings(arg))
  62.                 continue;
  63.             userDir = false;
  64.             printInfo(new File(arg));
  65.         }
  66.         if (userDir)
  67.             printInfo(new File(System.getProperty("user.dir")));
  68.     }
  69.  
  70.     public static boolean checkSettings(String option) {
  71.  
  72.         if (option == r) {
  73.             printInfo = true;
  74.             return true;
  75.         }
  76.         if (option == f) {
  77.             addSeparator = true;
  78.             return true;
  79.         }
  80.         if (option == d) {
  81.             lastTimeModify = true;
  82.             return true;
  83.         }
  84.         if (option == s) {
  85.             printSize = true;
  86.             return true;
  87.         }
  88.         return false;
  89.     }
  90.  
  91.     private static String getInformation(File file) {
  92.         StringBuilder sb = new StringBuilder();
  93.         if (printInfo)
  94.             sb.append(file.getAbsolutePath());
  95.         if (addSeparator)
  96.             if (!file.isFile())
  97.                 sb.append(file.getAbsolutePath() + File.separator);
  98.         if (lastTimeModify)
  99.             sb.append(" \nLast modification time: "
  100.                     + (new Date(file.lastModified())));
  101.         if (printSize)
  102.             sb.append(" \nByte Size: " + file.length() + " bytes");
  103.         sb.append(System.lineSeparator());
  104.         return sb.toString();
  105.     }
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement