今天跟着老师一起编写一个可以模拟DOS下一些功能的代码
import java.io.File;
public class CountFile {
static String[] exts = null;
public static void setExt(String ext) {
exts = ext.split(",");
}
public static boolean isExt(String name) {
for (String e : exts) {
if (name.toLowerCase().endsWith(e.toLowerCase())) {
return true;
}
}
return false;
}
public static void so(File f) {
if (f.isFile()) {
if (isExt(f.getName())) {
System.out.println(f.getPath());
}
} else {
File[] files = f.listFiles();
if (files != null) {
for (File file : files) {
so(file);
}
}
}
}
public static void main(String[] args) {
}
}
文件搜索代码
import java.io.File;
public class CountFileIf {
static String[] exts = null;
static String fuhao = "";
static long size = 0;
public static void setExt(String ext) {
if (ext.indexOf(">") > 0) {
exts = ext.split(">");
fuhao = ">";
} else if (ext.indexOf("<") > 0) {
exts = ext.split("<");
fuhao = "<";
} else if (ext.indexOf("=") > 0) {
exts = ext.split("=");
fuhao = "=";
}
size = Integer.parseInt(exts[1]);
}
public static boolean isExt(File f) {
if (f.getName().toLowerCase().endsWith(exts[0].toLowerCase())) {
if (fuhao.equals("=")) {
if (f.length() == size) {
return true;
}
} else if (fuhao.equals(">")) {
if (f.length() > size) {
return true;
}
} else if (fuhao.equals("<")) {
if (f.length() < size) {
return true;
}
}
}
return false;
}
public static void so(File f) {
if (f.isFile()) {
if (isExt(f)) {
System.out.println("[" + f.length() + "]" + f.getPath());
}
} else {
File[] files = f.listFiles();
if (files != null) {
for (File file : files) {
so(file);
}
}
}
}
public static void main(String[] args) {
// System.out.println(".dll=300".split("=").length);
}
}
Dos菜单代码
package com.lxl.mydos;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Scanner;
public class DOS {
public static void hy() { //打印程序刚运行界面
System.out.println(osName + " " + osVersion + " MyDOS [版本 1.1.2600]");
System.out.println("(C) 版权所有 2014-2015 Microsoft Corp.");
System.out.println("当前登录用户:" + userName);
}
static String osName = ""; //操作系统名
static String osVersion = ""; //操作系统版本
static String userHome = ""; //地址
static String userName = ""; //用户名
static {
osName = (System.getProperty("os.name"));
osVersion = (System.getProperty("os.version"));
userHome = (System.getProperty("user.home"));
userName = (System.getProperty("user.name"));
}
// 删除夹子
public static void deleteFile(File f) {
if (f.exists()) {
if (f.isFile()) {
if (f.delete()) {
System.out.println("[已删除]" + f.getPath());
} else {
System.out.println("[未删除]" + f.getPath());
}
} else if (f.isDirectory()) {
File[] files = f.listFiles();
for (File file : files) {
deleteFile(file);
}
f.delete();
}
}
}
public static void main(String[] args) {
hy();
Scanner input = new Scanner(System.in);
File path = new File(userHome);
while (true) {
System.out.print("\n" + path.getPath() + ">");
String line = input.nextLine();
if (line.trim().equalsIgnoreCase("exit")) { //退出
System.exit(0);
}
if (line.trim().toLowerCase().startsWith("del")) { //删除
File newPath = new File(path, line.split(" ")[1]);
if (newPath.isFile()) {
newPath.delete();
} else {
System.out.println("您确定要删除夹子吗?yes/no");
String jg = input.nextLine();
if (jg.equalsIgnoreCase("yes")) {
deleteFile(newPath);
}
}
} else if (line.trim().toLowerCase().startsWith("cd")) { //转盘
line = line.trim().toLowerCase();
if (line.equalsIgnoreCase("cd \\")) { //根盘符
String newPath = path.getPath().substring(0,
path.getPath().indexOf(":") + 2);
path = new File(newPath);
} else {
// cd 夹子
// cd ..
// cd .
String p1 = line.split(" ")[1];
if (p1.equals("..")) {
try {
path = path.getParentFile();
} catch (Exception ex) {
// System.out.println("已经到根目录 。");
}
} else {
File newPath = new File(path, p1);
if (newPath.exists()) {
path = newPath;
} else {
System.out.println("系统找不到指定的路径。");
}
}
}
} else if (line.trim().toLowerCase().startsWith("edit")) {
try {
Runtime.getRuntime().exec(
"notepad "
+ new File(path, line.split(" ")[1])
.getAbsolutePath());
} catch (Exception e) {
// TODO: handle exception
}
} else if (line.trim().toLowerCase().startsWith("create file")) {
try {
new File(path, line.split(" ")[2]).createNewFile();
System.out.println("创建成功!");
} catch (IOException e) {
System.out.println("目录不可以创建文件。");
}
} else if (line.trim().toLowerCase().startsWith("mk")) {
try {
new File(path, line.split(" ")[1]).mkdirs();
System.out.println("创建成功!");
} catch (Exception e) {
System.out.println("目录不可以创建夹子。");
}
} else if (line.trim().toLowerCase().startsWith("ls")) { // 做完
File[] files = path.listFiles();
int filecount = 0;
int dircount = 0;
long size = 0;
for (File file : files) {
if (file.isFile()) {
filecount++;
size += file.length();
} else {
dircount++;
}
System.out.println(new Date(file.lastModified())
.toLocaleString()
+ "\t"
+ (file.isDirectory() ? "<DIR>" : "")
+ "\t"
+ (file.length() == 0 ? "" : file.length())
+ "\t"
+ file.getName());
}
System.out.println("\t\t" + filecount + " 个文件\t\t " + size
+ " 字节");
System.out.println("\t\t" + dircount + " 个目录\t"
+ path.getTotalSpace() + " 可用字节");
} else if (line.trim().toLowerCase().startsWith("so")) {
CountFile.setExt(line.split(" ")[1]);
CountFile.so(path);
} else if (line.trim().toLowerCase().startsWith("sa")) {
CountFileIf.setExt(line.split(" ")[1]);
CountFileIf.so(path);
} else {
if (line.trim().indexOf(":") == line.length() - 1) {
File f = new File(line.toUpperCase() + "\\");
if (f.exists()) {
path = f;
} else {
System.out.println("不存在!");
}
} else {
System.out.println("'" + line + "' 不是内部或外部命令,也不是可运行的程序");
System.out.println("或批处理文件。");
}
}
}
}
}