[Java] 纯文本查看 复制代码 import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class MyDOS {
public static void main(String[] args) {
myDOS();
}
public static void myDOS() {
Scanner sc = new Scanner(System.in);
File now = new File("C:\\Users\\Administrator");
for(String line;;) {
System.out.print(now.getAbsolutePath() + ">");
line = sc.nextLine().trim();
if( line == null || "".equals(line) ) continue;
if( "exit".equalsIgnoreCase(line) ) break;
if( "list".equals(line) ) {
list(now);
}
else if( line.toLowerCase().startsWith("cd") ){
line = line.substring(2).trim();
if( line.matches("(\\.\\.?)((/\\.\\.)*(/\\.)*)*/?") ) {
int n = line.length() - line.replace("..", ".").length();
while( n-- > 0 ) {
File t = now.getParentFile();
if( t == null )
break;
now = t;
}
continue;
} else {
File f = null;
try {
f = new File(now, line).getCanonicalFile();
} catch (IOException e) {
e.printStackTrace();
}
if( f!=null && f.exists() && f.isDirectory() ) {
now = f;
}
continue;
}
} else {
System.out.println("不支持的命令: " + line);
}
}
System.out.println("over.");
}
public static void list(File root){
if( !root.exists() )
return;
if( root.isDirectory() ) {
for( File file : root.listFiles() )
System.out.println( file.getName() );
} else {
System.out.println( root.getName() );
}
}
}
大家多多交流^_^
|