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 = "\n\n1.Usage:MakeDirectories path1...\n" 17 + "\tCreates each path\n\n" 18 + "2.Usage:MakeDirectories -d path1...\n" 19 + "\tDeletes each path\n\n" 20 + "3.Usage:MakeDirectories -r path1 path2...\n" 21 + "\tRenames from path1 to path2\n\n"; 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("\n\nAbsolute 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("\n\n"); 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 ///:~

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwfgzy.html