如果你接触过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

 /**
*
*/
package com.b510.scanFiles.dao; /**
* the interface for the Scan Files
* @author Hongten
* @created Feb 12, 2014
*/
public interface ScanFiles { public int scan(String[] inputs);
public void handleCD(String[] inputs);
public void scanFiles(String[] inputs);
}

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

 /**
*
*/
package com.b510.scanFiles.dao.impl; import java.io.File; import com.b510.scanFiles.dao.ScanFiles;
import com.b510.scanFiles.utils.CommonUtil;
import com.b510.scanFiles.utils.PrintUtil; /**
* the Delegate for the Scan Files.
* @author Hongten
* @created Feb 12, 2014
*/
public class ScanDelegate implements ScanFiles, Runnable { @SuppressWarnings("unused")
private String[] inputs;
private String path;
private String suffix;
private int targetCount;
private int folderCount;
private long startTime; public ScanDelegate(String[] inputs) {
this.inputs = inputs;
} public ScanDelegate(String path, String suffix) {
this.path = path;
this.suffix = suffix;
} public void scanFiles(String[] inputs) {
ScanDelegate delegate = new ScanDelegate(inputs[0], inputs[1]);
new Thread(delegate).start();
} /**
* scan the file(s) from the path,that user give.and return the count of the file(s).
*/
public int scan(String[] inputs) {
int fileCount = 0;
File file = new File(inputs[0]);
if (!file.exists()) {
PrintUtil.printInfo(CommonUtil.INCORRECT_PATH);
} else {
File[] subFile = file.listFiles();
if (null == subFile || subFile.length == 0) {
return fileCount;
}
for (int i = 0; i < subFile.length; i++) {
if (subFile[i].isDirectory()) {
folderCount++;
fileCount += scan(new String[] { subFile[i].getAbsolutePath() });
} else if (subFile[i].isFile()) {
if (getPostfix(subFile[i].getName()).equalsIgnoreCase(suffix)) {
PrintUtil.printInfo(CommonUtil.FIND_FILE + subFile[i].getAbsolutePath());
targetCount++;
}
fileCount++;
}
}
}
return fileCount;
} public void run() {
startTime = System.currentTimeMillis();
int num = scan(new String[] { path });
printResult(num);
} private void printResult(int num) {
PrintUtil.printInfo(CommonUtil.BOUNDARY);
PrintUtil.printInfo(CommonUtil.LEFT_BLANKETS + path + CommonUtil.CONTAINS);
PrintUtil.printInfo(CommonUtil.FOLDERS + folderCount);
PrintUtil.printInfo(CommonUtil.FILES + num);
PrintUtil.printInfo(CommonUtil.TARGET + suffix + CommonUtil.TARGET_FILES + targetCount);
PrintUtil.printInfo(CommonUtil.BOUNDARY);
long entTime = System.currentTimeMillis();
PrintUtil.printInfo(CommonUtil.SPEND + (entTime - startTime) + CommonUtil.MS);
} /**
* handle the situation,e.g: "cd e:\"
*/
public void handleCD(String[] inputs) {
File file = new File(inputs[1]);
if (!file.exists()) {
PrintUtil.printInfo(CommonUtil.INCORRECT_PATH);
} else {
File[] listFiles = file.listFiles();
for (int i = 0; i < listFiles.length; i++) {
PrintUtil.printInfo(listFiles[i].getName());
}
PrintUtil.printInfo(CommonUtil.BOUNDARY + "\n"+ CommonUtil.TOTAL + listFiles.length);
}
} /**
* get the suffix of a string,e.g : "test.txt"<br>
* and return the string is "txt".
* @param inputFilePath
* @return
*/
public String getPostfix(String inputFilePath) {
return inputFilePath.substring(inputFilePath.lastIndexOf(CommonUtil.POINT) + 1);
}
}

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

 /**
*
*/
package com.b510.scanFiles.dao.impl; import com.b510.scanFiles.dao.ScanFiles;
import com.b510.scanFiles.utils.CommonUtil;
import com.b510.scanFiles.utils.PrintUtil; /**
* @author Hongten
* @created Feb 12, 2014
*/
public class ScanProxy implements ScanFiles { private ScanFiles scanDelegate; public ScanProxy(ScanFiles scanDelegate) {
this.scanDelegate = scanDelegate;
} public int scan(String[] inputs) {
scanFiles(inputs);
return 0;
} public void handleCD(String[] inputs) {
PrintUtil.printInfo(CommonUtil.BOUNDARY);
long startTime = System.currentTimeMillis();
scanDelegate.handleCD(inputs);
long entTime = System.currentTimeMillis();
PrintUtil.printInfo(CommonUtil.BOUNDARY);
PrintUtil.printInfo(CommonUtil.SPEND + (entTime - startTime) + CommonUtil.MS);
} public void scanFiles(String[] inputs) {
scanDelegate.scanFiles(inputs);
} }

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

 /**
*
*/
package com.b510.scanFiles.factory; import com.b510.scanFiles.dao.ScanFiles;
import com.b510.scanFiles.dao.impl.ScanDelegate;
import com.b510.scanFiles.dao.impl.ScanProxy; /**
* The factory of the static proxy
* @author Hongten
* @created Feb 12, 2014
*/
public class ScanFilesFactory { public static ScanFiles getInstance(String[] inputs){
return new ScanProxy(new ScanDelegate(inputs));
}
}

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

 /**
*
*/
package com.b510.scanFiles.utils; /**
* @author Hongten
* @created Feb 12, 2014
*/
public class CommonUtil { public static final String DESCRIPTION = "Please input as this format :\n[C:\\ png] or [cd C:\\]";
public static final String EXIT_SYSTEM = "exited system.";
public static final String INCORRECT_PATH = "Incorrect path!";
public static final String INCORRECT_INPUT_FORMAT = "Incorrect input format!";
public static final String CD = "cd";
public static final String BLANK = " ";
public static final String EXIT = "exit";
public static final String POINT = "."; public static final String BOUNDARY = "======================================";
public static final String LEFT_BLANKETS = "[";
public static final String CONTAINS = "] contains :";
public static final String FOLDERS = "folder(s) : ";
public static final String FILES = "file(s) : ";
public static final String TARGET = "target [";
public static final String TARGET_FILES = "] file(s) : ";
public static final String SPEND = "Spend :[";
public static final String FIND_FILE = "Find file : ";
public static final String MS = "]ms";
public static final String TOTAL = "Total :"; public static final int TWO = 2;
}

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

 /**
*
*/
package com.b510.scanFiles.utils; /**
* @author Hongten
* @created Feb 12, 2014
*/
public class PrintUtil { public static void printInfo(String info) {
System.out.println(info);
} public static void printInfo(StringBuffer info) {
System.out.println(info);
} public static void printInfo(char info) {
System.out.println(info);
}
}

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

 /**
*
*/
package com.b510.scanFiles.client; import java.util.Scanner; import com.b510.scanFiles.dao.ScanFiles;
import com.b510.scanFiles.factory.ScanFilesFactory;
import com.b510.scanFiles.utils.CommonUtil;
import com.b510.scanFiles.utils.PrintUtil; /**
* The Client class,User can input some content in the console.<br>
* and the system will monitoring the console and get the content and process it.
* @author Hongten
* @created Feb 12, 2014
*/
public class Client { private Scanner inputStreamScanner;
private boolean controlFlag = false; public static void main(String[] args) {
Client client = new Client();
client.monitoringConsoleAndHandleContent();
} public Client() {
inputStreamScanner = new Scanner(System.in);
} /**
* This is a monitor,which can monitoring the console and handle the input content.
*/
public void monitoringConsoleAndHandleContent() {
PrintUtil.printInfo(CommonUtil.DESCRIPTION);
try {
while (!controlFlag && inputStreamScanner.hasNext()) {
processingConsoleInput();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
closeScanner();
}
} private void closeScanner() {
inputStreamScanner.close();
} /**
* Processing the content that user inputed.
*/
private void processingConsoleInput() {
String inputContent = inputStreamScanner.nextLine();
if (!inputContent.isEmpty()) {
controlFlag = exit(inputContent);
if (!controlFlag) {
notExit(inputContent);
}
}
} /**
* When the user input the keyword <code>"exit"</code>(ignore case),then the system will be exit.
* @param inputContent
*/
private boolean exit(String inputContent) {
if (CommonUtil.EXIT.equalsIgnoreCase(inputContent)) {
controlFlag = true;
systemExit();
}
return controlFlag;
} /**
* System.exit(0);
*/
private void systemExit() {
PrintUtil.printInfo(CommonUtil.EXIT_SYSTEM);
System.exit(0);
} /**
* different with the method "exit()"
* @param inputContent
*/
private void notExit(String inputContent) {
String[] inputs = inputContent.split(CommonUtil.BLANK);
if (null != inputs && inputs.length >= CommonUtil.TWO) {
//the factory of the proxy
ScanFiles factory = ScanFilesFactory.getInstance(inputs);
if (CommonUtil.CD.equalsIgnoreCase(inputs[0])) {
factory.handleCD(inputs);
} else {
factory.scan(inputs);
}
} else {
PrintUtil.printInfo(CommonUtil.INCORRECT_INPUT_FORMAT);
}
}
}

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

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

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

原创Java版的Shell的更多相关文章

  1. 常见排序算法题(java版)

    常见排序算法题(java版) //插入排序:   package org.rut.util.algorithm.support;   import org.rut.util.algorithm.Sor ...

  2. Jaeger的客户端采样配置(Java版)

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  3. java版的类似飞秋的局域网在线聊天项目

    原文链接:http://www.cnblogs.com/wangleiblog/articles/5323305.html 转载请注明 最近在弄一个java版的局域网在线聊天项目,功能跟飞秋差不多.p ...

  4. 排序算法Java版,以及各自的复杂度,以及由堆排序产生的top K问题

    常用的排序算法包括: 冒泡排序:每次在无序队列里将相邻两个数依次进行比较,将小数调换到前面, 逐次比较,直至将最大的数移到最后.最将剩下的N-1个数继续比较,将次大数移至倒数第二.依此规律,直至比较结 ...

  5. 微博excel数据清洗(Java版)

    微博数据清洗(Java版) 原创 2013年12月10日 10:58:24 2979 大数据公益大学提供的一份数据,义务处理一下,原始数据是Excel,含有html标签,如下:   要求清洗掉html ...

  6. java版gRPC实战之一:用proto生成代码

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  7. java版gRPC实战之二:服务发布和调用

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  8. java版gRPC实战之三:服务端流

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  9. java版gRPC实战之四:客户端流

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

随机推荐

  1. ubuntu12 apache2部署多个网站的方法

    由于对apache2的配置不是很精通.吃了不少瘪. 这里总结下,希望对碰到同样问题的人带来帮助: 假设需求是有两个站点,site1和site2.其端口分别为80和8080. 1. 配置监听端口 修改 ...

  2. python - PyQuery

    偶尔的机会,知道这么个扩展,手贱翻了下文档,发现似乎挺有意思,遂记录一二. what: 这是一个python版本的jquery,而且是后端执行的,至少官方是这么说的: pyquery allows y ...

  3. c++11 gcc4.8.x安装

    c++11 gcc4.8.x安装 2014年12月11日默北   要安装PHP-CPP,需要c++11,否则就会报错,错误信息如下所示: g++ -Wall -c -g -std=c++11 -fpi ...

  4. 【GoLang】GoLang fmt 占位符详解

    golang 的fmt 包实现了格式化I/O函数,类似于C的 printf 和 scanf. # 定义示例类型和变量 type Human struct { Name string } var peo ...

  5. ASP.NET小知识

    所有System.Web.UI.*命名空间下的内容可以称为Web From,而System.Web.*命名空间下的其他内容可以称为ASP.NET. @section用法:配合母版页中的@RenderS ...

  6. 利用cocostudio库函数 实现左右滑动的背包栏UI (cocos2d-x 2.2.0)

    .h #ifndef __COMMON_COMPONENTS__ #define __COMMON_COMPONENTS__ #include "cocos2d.h" #inclu ...

  7. smarty模版出现错误提示出现了不期望的字符

    2013年7月5日 08:38:49 提示 unexpected "字符或字符串" 查找前边的代码,看是否有字符串单引号或双引号没有成对出现的情况

  8. mysql源码:关于innodb中两次写的探索

    两次写可以说是在Innodb中很独特的一个功能点,而关于它的说明或者解释非常少,至于它存在的原因更没有多少文章来说,所以我打算专门对它做一次说明. 首先说明一下为什么会有两次写这个东西:因为innod ...

  9. 解决Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51.0

    jdk问题 解决方案: 更换IDE的jdk

  10. C/C++函数参数读取顺序2

    #include <stdio.h>int main(){ int a=1,b=3,c=3; printf("%d, %d, %d",(a++,b++,c++),b,c ...