原创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
/**
*
*/
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的更多相关文章
- 常见排序算法题(java版)
常见排序算法题(java版) //插入排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.Sor ...
- Jaeger的客户端采样配置(Java版)
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- java版的类似飞秋的局域网在线聊天项目
原文链接:http://www.cnblogs.com/wangleiblog/articles/5323305.html 转载请注明 最近在弄一个java版的局域网在线聊天项目,功能跟飞秋差不多.p ...
- 排序算法Java版,以及各自的复杂度,以及由堆排序产生的top K问题
常用的排序算法包括: 冒泡排序:每次在无序队列里将相邻两个数依次进行比较,将小数调换到前面, 逐次比较,直至将最大的数移到最后.最将剩下的N-1个数继续比较,将次大数移至倒数第二.依此规律,直至比较结 ...
- 微博excel数据清洗(Java版)
微博数据清洗(Java版) 原创 2013年12月10日 10:58:24 2979 大数据公益大学提供的一份数据,义务处理一下,原始数据是Excel,含有html标签,如下: 要求清洗掉html ...
- java版gRPC实战之一:用proto生成代码
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- java版gRPC实战之二:服务发布和调用
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- java版gRPC实战之三:服务端流
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- java版gRPC实战之四:客户端流
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
随机推荐
- php增加对mysqli的支持
php增加对mysqli的支持 我在fedora下使用yum安装的php和mysql,但是发现php不支持myslqi,只能编译一个mysqli的扩展给php用了. 方法如下: 1.下载php 2 ...
- ali2015校园招聘笔试大题
[本文链接] http://www.cnblogs.com/hellogiser/p/ali-2015-questions.html 1. 写一个函数,输入一个二叉树,树中每个节点存放了一个整数值,函 ...
- 36.在字符串中删除特定的字符[Delete source from dest]
[题目] 输入两个字符串,从第一字符串中删除第二个字符串中所有的字符.例如,输入”They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”. ...
- ORACLE清除某一字段重复的数据(选取重复数据中另一个字段时期最大值)
需求:资产维修表中同一资产可能维修完继续申请维修,这时候维修状态需要根据最近的维修时间去判断维修状态,所以同一资产ID下会出现重复的数据(维修审批通过,维修审批未通过),或者可能不出现(未申请维修), ...
- 【数据结构】hanoi
#include<stdio.h> void hanoi(int n,char x,char y,char z) { ; ) printf("%d. Move disk %d f ...
- 两个文件去重的N种姿势
最近利用shell帮公司优化挖掘关键词的流程,用shell替代了多个环节的操作,极大提高了工作效率. shell在文本处理上确有极大优势,比如多文本合并.去重等,但是最近遇到了一个难搞的问题,即两个大 ...
- CodeForces - 404A(模拟题)
Valera and X Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit ...
- input框颜色修该
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- PA
[题目描述] 汉诺塔升级了:现在我们有?个圆盘和?个柱子,每个圆盘大小都不一样, 大的圆盘不能放在小的圆盘上面,?个柱子从左到右排成一排.每次你可以将一 个柱子上的最上面的圆盘移动到右边或者左边的柱子 ...
- 一、HTML和CSS基础--网页布局--实践--固定层效果
absolute和fixed的相同点: 第一,完全脱离标准文档流 第二,未设置偏移量时,都定位在父元素的左上角 absolute和fixed的不同点: 第一.当设置偏移量时,偏移参照基准不同 abso ...