原創Java版的Shell


如果你接觸過windows操作系統,你應該對windows中的cmd有一定的了解。

如果你接觸過Linux操作系統,你應該對Linux的shell有一定的了解。

本文說的正是linux中的shell。不過這個是我用java編程實現的“shell”。

現在的功能有三個:

   1.掃描文件(過濾文件),如:“c:\\ gif”,命令是在C盤下面,查找后綴是.gif的文件,並且打印到控制台上。

   2.命令:“cd c:\\users”,把路徑“c:\\users"下面的所有文件打印出來,並且進行統計。

   3.命令:“exit”,退出系統。

先來看看項目的結構:

運行的效果:

one:

two:

three:

====================================================

Original Source Part!

Description:

I use the static proxy design pattern in this java project.When the user input 

content in the console,then the system will monitoring and get the content,and

process it quickly.

The original source as following:

====================================================

/scanFiles/src/com/b510/scanFiles/dao/ScanFiles.java

 1 /**
 2  * 
 3  */
 4 package com.b510.scanFiles.dao;
 5 
 6 /**
 7  * the interface for the Scan Files
 8  * @author Hongten
 9  * @created Feb 12, 2014
10  */
11 public interface ScanFiles {
12 
13     public int scan(String[] inputs);
14     public void handleCD(String[] inputs);
15     public void scanFiles(String[] inputs);
16 }

/scanFiles/src/com/b510/scanFiles/dao/impl/ScanDelegate.java

  1 /**
  2  * 
  3  */
  4 package com.b510.scanFiles.dao.impl;
  5 
  6 import java.io.File;
  7 
  8 import com.b510.scanFiles.dao.ScanFiles;
  9 import com.b510.scanFiles.utils.CommonUtil;
 10 import com.b510.scanFiles.utils.PrintUtil;
 11 
 12 /**
 13  * the Delegate for the Scan Files.
 14  * @author Hongten
 15  * @created Feb 12, 2014
 16  */
 17 public class ScanDelegate implements ScanFiles, Runnable {
 18 
 19     @SuppressWarnings("unused")
 20     private String[] inputs;
 21     private String path;
 22     private String suffix;
 23     private int targetCount;
 24     private int folderCount;
 25     private long startTime;
 26 
 27     public ScanDelegate(String[] inputs) {
 28         this.inputs = inputs;
 29     }
 30 
 31     public ScanDelegate(String path, String suffix) {
 32         this.path = path;
 33         this.suffix = suffix;
 34     }
 35 
 36     public void scanFiles(String[] inputs) {
 37         ScanDelegate delegate = new ScanDelegate(inputs[0], inputs[1]);
 38         new Thread(delegate).start();
 39     }
 40 
 41     /**
 42      * scan the file(s) from the path,that user give.and return the count of the file(s).
 43      */
 44     public int scan(String[] inputs) {
 45         int fileCount = 0;
 46         File file = new File(inputs[0]);
 47         if (!file.exists()) {
 48             PrintUtil.printInfo(CommonUtil.INCORRECT_PATH);
 49         } else {
 50             File[] subFile = file.listFiles();
 51             if (null == subFile || subFile.length == 0) {
 52                 return fileCount;
 53             }
 54             for (int i = 0; i < subFile.length; i++) {
 55                 if (subFile[i].isDirectory()) {
 56                     folderCount++;
 57                     fileCount += scan(new String[] { subFile[i].getAbsolutePath() });
 58                 } else if (subFile[i].isFile()) {
 59                     if (getPostfix(subFile[i].getName()).equalsIgnoreCase(suffix)) {
 60                         PrintUtil.printInfo(CommonUtil.FIND_FILE + subFile[i].getAbsolutePath());
 61                         targetCount++;
 62                     }
 63                     fileCount++;
 64                 }
 65             }
 66         }
 67         return fileCount;
 68     }
 69 
 70     public void run() {
 71         startTime = System.currentTimeMillis();
 72         int num = scan(new String[] { path });
 73         printResult(num);
 74     }
 75 
 76     private void printResult(int num) {
 77         PrintUtil.printInfo(CommonUtil.BOUNDARY);
 78         PrintUtil.printInfo(CommonUtil.LEFT_BLANKETS + path + CommonUtil.CONTAINS);
 79         PrintUtil.printInfo(CommonUtil.FOLDERS + folderCount);
 80         PrintUtil.printInfo(CommonUtil.FILES + num);
 81         PrintUtil.printInfo(CommonUtil.TARGET + suffix + CommonUtil.TARGET_FILES + targetCount);
 82         PrintUtil.printInfo(CommonUtil.BOUNDARY);
 83         long entTime = System.currentTimeMillis();
 84         PrintUtil.printInfo(CommonUtil.SPEND + (entTime - startTime) + CommonUtil.MS);
 85     }
 86 
 87     /**
 88      * handle the situation,e.g: "cd e:\"
 89      */
 90     public void handleCD(String[] inputs) {
 91         File file = new File(inputs[1]);
 92         if (!file.exists()) {
 93             PrintUtil.printInfo(CommonUtil.INCORRECT_PATH);
 94         } else {
 95             File[] listFiles = file.listFiles();
 96             for (int i = 0; i < listFiles.length; i++) {
 97                 PrintUtil.printInfo(listFiles[i].getName());
 98             }
 99             PrintUtil.printInfo(CommonUtil.BOUNDARY + "\n"+ CommonUtil.TOTAL + listFiles.length);
100         }
101     }
102 
103     /**
104      * get the suffix of a string,e.g : "test.txt"<br>
105      * and return the string is "txt".
106      * @param inputFilePath
107      * @return
108      */
109     public String getPostfix(String inputFilePath) {
110         return inputFilePath.substring(inputFilePath.lastIndexOf(CommonUtil.POINT) + 1);
111     }
112 }

/scanFiles/src/com/b510/scanFiles/dao/impl/ScanProxy.java

 1 /**
 2  * 
 3  */
 4 package com.b510.scanFiles.dao.impl;
 5 
 6 import com.b510.scanFiles.dao.ScanFiles;
 7 import com.b510.scanFiles.utils.CommonUtil;
 8 import com.b510.scanFiles.utils.PrintUtil;
 9 
10 /**
11  * @author Hongten
12  * @created Feb 12, 2014
13  */
14 public class ScanProxy implements ScanFiles {
15 
16     private ScanFiles scanDelegate;
17 
18     public ScanProxy(ScanFiles scanDelegate) {
19         this.scanDelegate = scanDelegate;
20     }
21 
22     public int scan(String[] inputs) {
23         scanFiles(inputs);
24         return 0;
25     }
26 
27     public void handleCD(String[] inputs) {
28         PrintUtil.printInfo(CommonUtil.BOUNDARY);
29         long startTime = System.currentTimeMillis();
30         scanDelegate.handleCD(inputs);
31         long entTime = System.currentTimeMillis();
32         PrintUtil.printInfo(CommonUtil.BOUNDARY);
33         PrintUtil.printInfo(CommonUtil.SPEND + (entTime - startTime) + CommonUtil.MS);
34     }
35 
36     public void scanFiles(String[] inputs) {
37         scanDelegate.scanFiles(inputs);
38     }
39 
40 }

/scanFiles/src/com/b510/scanFiles/factory/ScanFilesFactory.java

 1 /**
 2  * 
 3  */
 4 package com.b510.scanFiles.factory;
 5 
 6 import com.b510.scanFiles.dao.ScanFiles;
 7 import com.b510.scanFiles.dao.impl.ScanDelegate;
 8 import com.b510.scanFiles.dao.impl.ScanProxy;
 9 
10 /**
11  * The factory of the static proxy
12  * @author Hongten
13  * @created Feb 12, 2014
14  */
15 public class ScanFilesFactory {
16 
17     public static ScanFiles getInstance(String[] inputs){
18         return new ScanProxy(new ScanDelegate(inputs));
19     }
20 }

/scanFiles/src/com/b510/scanFiles/utils/CommonUtil.java

 1 /**
 2  * 
 3  */
 4 package com.b510.scanFiles.utils;
 5 
 6 /**
 7  * @author Hongten
 8  * @created Feb 12, 2014
 9  */
10 public class CommonUtil {
11 
12     public static final String DESCRIPTION = "Please input as this format :\n[C:\\ png] or [cd C:\\]";
13     public static final String EXIT_SYSTEM = "exited system.";
14     public static final String INCORRECT_PATH = "Incorrect path!";
15     public static final String INCORRECT_INPUT_FORMAT = "Incorrect input format!";
16     public static final String CD = "cd";
17     public static final String BLANK = " ";
18     public static final String EXIT = "exit";
19     public static final String POINT = ".";
20     
21     public static final String BOUNDARY = "======================================";
22     public static final String LEFT_BLANKETS = "[";
23     public static final String CONTAINS = "]  contains :";
24     public static final String FOLDERS = "folder(s)              : ";
25     public static final String FILES = "file(s)                : ";
26     public static final String TARGET = "target [";
27     public static final String TARGET_FILES = "] file(s)  : ";
28     public static final String SPEND = "Spend :[";
29     public static final String FIND_FILE = "Find file : ";
30     public static final String MS = "]ms";
31     public static final String TOTAL = "Total :";
32 
33     
34     public static final int TWO = 2;
35 }

/scanFiles/src/com/b510/scanFiles/utils/PrintUtil.java

 1 /**
 2  * 
 3  */
 4 package com.b510.scanFiles.utils;
 5 
 6 /**
 7  * @author Hongten
 8  * @created Feb 12, 2014
 9  */
10 public class PrintUtil {
11 
12     public static void printInfo(String info) {
13         System.out.println(info);
14     }
15 
16     public static void printInfo(StringBuffer info) {
17         System.out.println(info);
18     }
19 
20     public static void printInfo(char info) {
21         System.out.println(info);
22     }
23 }

/scanFiles/src/com/b510/scanFiles/client/Client.java

  1 /**
  2  * 
  3  */
  4 package com.b510.scanFiles.client;
  5 
  6 import java.util.Scanner;
  7 
  8 import com.b510.scanFiles.dao.ScanFiles;
  9 import com.b510.scanFiles.factory.ScanFilesFactory;
 10 import com.b510.scanFiles.utils.CommonUtil;
 11 import com.b510.scanFiles.utils.PrintUtil;
 12 
 13 /**
 14  * The Client class,User can input some content in the console.<br>
 15  * and the system will monitoring the console and get the content and process it.
 16  * @author Hongten
 17  * @created Feb 12, 2014
 18  */
 19 public class Client {
 20 
 21     private Scanner inputStreamScanner;
 22     private boolean controlFlag = false;
 23 
 24     public static void main(String[] args) {
 25         Client client = new Client();
 26         client.monitoringConsoleAndHandleContent();
 27     }
 28 
 29     public Client() {
 30         inputStreamScanner = new Scanner(System.in);
 31     }
 32 
 33     /**
 34      * This is a monitor,which can monitoring the console and handle the input content.
 35      */
 36     public void monitoringConsoleAndHandleContent() {
 37         PrintUtil.printInfo(CommonUtil.DESCRIPTION);
 38         try {
 39             while (!controlFlag && inputStreamScanner.hasNext()) {
 40                 processingConsoleInput();
 41             }
 42         } catch (Exception e) {
 43             e.printStackTrace();
 44         } finally {
 45             closeScanner();
 46         }
 47     }
 48 
 49     private void closeScanner() {
 50         inputStreamScanner.close();
 51     }
 52 
 53     /**
 54      * Processing the content that user inputed.
 55      */
 56     private void processingConsoleInput() {
 57         String inputContent = inputStreamScanner.nextLine();
 58         if (!inputContent.isEmpty()) {
 59             controlFlag = exit(inputContent);
 60             if (!controlFlag) {
 61                 notExit(inputContent);
 62             }
 63         }
 64     }
 65 
 66     /**
 67      * When the user input the keyword <code>"exit"</code>(ignore case),then the system will be exit.
 68      * @param inputContent
 69      */
 70     private boolean exit(String inputContent) {
 71         if (CommonUtil.EXIT.equalsIgnoreCase(inputContent)) {
 72             controlFlag = true;
 73             systemExit();
 74         }
 75         return controlFlag;
 76     }
 77 
 78     /**
 79      * System.exit(0);
 80      */
 81     private void systemExit() {
 82         PrintUtil.printInfo(CommonUtil.EXIT_SYSTEM);
 83         System.exit(0);
 84     }
 85 
 86     /**
 87      * different with the method "exit()"
 88      * @param inputContent
 89      */
 90     private void notExit(String inputContent) {
 91         String[] inputs = inputContent.split(CommonUtil.BLANK);
 92         if (null != inputs && inputs.length >= CommonUtil.TWO) {
 93             //the factory of the proxy
 94             ScanFiles factory = ScanFilesFactory.getInstance(inputs);
 95             if (CommonUtil.CD.equalsIgnoreCase(inputs[0])) {
 96                 factory.handleCD(inputs);
 97             } else {
 98                 factory.scan(inputs);
 99             }
100         } else {
101             PrintUtil.printInfo(CommonUtil.INCORRECT_INPUT_FORMAT);
102         }
103     }
104 }

===========================================

Original Source Doaload:  http://files.cnblogs.com/hongten/scanFiles.zip

===========================================

 

========================================================

 

More reading,and english is important.

 

I'm Hongten

 

 

 

大哥哥大姐姐,覺得有用打賞點哦!多多少少沒關系,一分也是對我的支持和鼓勵。謝謝。
Hongten博客排名在100名以內。粉絲過千。
Hongten出品,必是精品。

 

E | hongtenzone@foxmail.com  B | http://www.cnblogs.com/hongten

 

========================================================

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM