zoukankan      html  css  js  c++  java
  • File类的深度学习(超简易文件管理器)——《Thinking in Java》随笔028

      1 //: MakeDirectories.java
      2 package c10;
      3 
      4 import java.io.File;
      5 import java.text.SimpleDateFormat;
      6 import java.util.Date;
      7 
      8 /**
      9 *    @time:         下午7:59:23
     10 *    @date:         2017年4月29日
     11 *    @auther:    skyfffire
     12 *    @version:    v0.1
     13 */
     14 public class MakeDirectories {
     15     private final static String usage 
     16         = "
    
    1.Usage:MakeDirectories path1...
    "
     17         + "	Creates each path
    
    "
     18         + "2.Usage:MakeDirectories -d path1...
    "
     19         + "	Deletes each path
    
    "
     20         + "3.Usage:MakeDirectories -r path1 path2...
    "
     21         + "	Renames from path1 to path2
    
    ";
     22     
     23     /**
     24      * 使用方法
     25      */
     26     private static void usage() {
     27         System.out.println(usage);
     28         System.exit(1);
     29     }
     30     
     31     /**
     32      * 显示一个path的详细信息
     33      * 
     34      * @param f 需要显示的path
     35      */
     36     private static void fileData(File f) {
     37         System.out.println("
    
    Absolute path:" + f.getAbsolutePath());
     38         System.out.println("Can read:" + f.canRead());
     39         System.out.println("Can Write:" + f.canWrite());
     40         System.out.println("getName:" + f.getName());
     41         System.out.println("getParent:" + f.getParent());
     42         System.out.println("getPath:" + f.getPath());
     43         System.out.println("length:" + f.length());        
     44         System.out.println("lastModified:" 
     45         + new SimpleDateFormat("yy-MM-dd").
     46         format(new Date(f.lastModified())));
     47         
     48         if (f.isFile()) {
     49             System.out.println("It's a file.");
     50         } else if (f.isDirectory()) {
     51             System.out.println("It's a directory.");
     52         }
     53         
     54         System.out.println("
    
    ");
     55     }
     56     
     57     /**
     58      * 打印当前目录下的所有path
     59      */
     60     private static void printPath() {
     61         System.out.println();
     62         
     63         File point = new File(".");
     64         
     65         String[] path = point.list();
     66         
     67         for (String nowString : path) {
     68             System.out.println(nowString);
     69         }
     70         
     71         System.out.println();
     72     }
     73     
     74     public static void main(String[] args) {
     75         printPath();
     76         
     77         if (args.length < 1) {
     78             usage();
     79         }
     80         
     81         // 更新path名字命令
     82         if (args[0].equals("-r")) {
     83             if (args.length != 3) {
     84                 usage();
     85             } else {
     86                 File old = new File(args[1]);
     87                 File rname = new File(args[2]);
     88                 
     89                 if (old.exists() && !rname.exists()) {
     90                     // 更新文件名
     91                     old.renameTo(rname);
     92                 } else {
     93                     System.out.println("rename error: "
     94                             + old.getName() + " to " + rname.getName());
     95                 }
     96                 
     97                 // 打印信息
     98                 fileData(old);
     99                 fileData(rname);
    100             }
    101         } // 执行删除命令
    102         else if (args[0].equals("-d")) {
    103             for (int count = 1; count < args.length; count++) {
    104                 File f = new File(args[count]);
    105                 
    106                 // 判断f是否存在
    107                 if (f.exists()) {
    108                     System.out.println(f + " exists");
    109                     
    110                     System.out.println("deleting..." + f);
    111                     f.delete();
    112 
    113                     fileData(f);
    114                 } else {
    115                     System.out.println(f + " path is not found.");
    116                 }
    117             }
    118         } // 执行创建命令
    119         else {
    120             for (int count = 0; count < args.length; count++) {
    121                 File f = new File(args[count]);
    122                 
    123                 f.mkdirs();
    124                 // f.mkdir()与f.mkdirs()的区别在于
    125                 // mkdir不能在不存在的目录创建文件夹
    126                 
    127                 System.out.println("created " + f);
    128 
    129                 fileData(f);
    130             }
    131         }
    132         
    133         printPath();
    134     }
    135 }
    136 
    137 ///:~

    优化了原书中某些不合理的地方。运行时将参数列表置于java xxx 之后即可。

  • 相关阅读:
    语音识别六十年
    神经网络架构PYTORCH-几个概念
    Ubuntu 16.04 系统无法挂载u盘的问题
    技术的止境
    神经网络架构PYTORCH-宏观分析
    Python中parameters与argument区别
    神经网络架构PYTORCH-初相识(3W)
    【ES】学习9-聚合2
    【ES】学习8-聚合1
    【python】中文提取,判断,分词
  • 原文地址:https://www.cnblogs.com/skyfffire/p/6786238.html
Copyright © 2011-2022 走看看