cool hit counter The original Java version of the Shell_Intefrankly

The original Java version of the Shell


If you have come across windows operating system, you should have some knowledge of cmd in windows.

If you have been exposed to the Linux operating system, you should have some knowledge of the Linux shell.

This article is talking about exactly the shell in linux. But this is a "shell" that I have implemented in java programming.

There are now three functions.

1.Scan the file (filter the file), e.g. "c:\ gif", the command is under the C drive, look for files with the .gif suffix and print them to the console.

2.command:“cd c:\users”, Put the path“c:\users" Print out all the documents below, And do the statistics。

3.Command: "exit" to exit the system.

Let's start by looking at the structure of the project.

The effect of running.

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 + "
"+ 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 :
[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

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


Recommended>>
1、The child who speaks the truth in The Emperors New ClothesBook Summary of The Wind Blows South of the River Internet Finance II
2、2017 Internet Consumer Finance Industry Golden Shrimp Award Winner fast loans
3、New upgrades to Say it and Go
4、A major boon Efforts to promote the innovation and development of big data industry in Shanxi
5、Li Xiaohan Yun ZhiSheng AI technology offers new possibilities for education

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号