How to use Java to implement the doc command line? DIR Displays the file CD file directory under all the current directories to enter the directory CD .. back to the parent directory. On the one hand, I wrote the code and debugged it. However, a small bug is that when you enter a file, a null pointer exception will be prompted, instead of prompting "not a directory" like the doc, but will not exit, if you want to fix this bug, you can modify package cmd; import Java. util. *; import Java. io. *;/** use Java to implement the doc command line * dir to display the * CD file directory under all directories to enter the * CD directory .. Return to parent directory
* Brief thought; * class. forname (), pass a class name to implement the multi-state operation * use the stack record to enter the directory, later, exit first. * if there are other better methods, you can discuss with each other */public class release test {/*** @ Param ARGs * @ throws exception * @ throws illegalaccessexception * @ throws instantiationexception */ public static void main (string [] ARGs) throws
Instantiationexception, illegalaccessexception, exception {// todo auto-generated method stub stack <string> stack = new stack <string> (); // stack record path, then, the kernel scan = new kernel (system. in); stack. push ("E:/"); // push the disk e path to the stack. You can change it to another drive C, drive D, while (true) {If (stack. isempty ()){
Stack. Push ("E:/"); // prevents CD .. Exit to stack empty} system. out. print (stack. peek (). tostring () + ">"); string srcname = scan. nextline (); string [] paths = srcname. split ("\ s +", 2); // cut the command statement into two blank parts, such as CD workspace into character arrays CD and workspace command COM = (command) class. forname ("cmd. "+ paths [0]. touppercase (). trim ()). newinstance (); // generate and execute commands, with Polymorphism
If (paths. length> 1) {// If the input is CD workspace, transfer the workspace to para
Com.exe cute (stack, paths [1]);} else com.exe cute (stack) ;}}// cmd command interface command {public void execute (stack <string> stack, string... para); // stack can record the directory path, para as a variable parameter} // the Dir command displays a directory file, class dir implements command {@ override public void execute (stack <string>
Stack, string... para) {If (para. length> 0) stack. push (stack. peek (). tostring () + para [0] + "/"); // file dir = new file (stack. peek (). tostring (); // If (! Dir. exists () {system. Out. println ("this directory does not exist");} file [] files = dir. listfiles (); For (File file: Files ){
System. out. println (File); // display all files in the directory }}// enter a directory named cd in the CD workspace .. exit the last parent directory class CD implements command {@ override
Public void execute (stack <string> stack, string... para) {If (para. length> 0) {// todo auto-generated method stub if (para [0]. equals (".. ". trim () {// CD .. stack. pop (); // exit stack and exit from the previous directory} else {stack. push (stack. peek (). tostring () + para [0] + "/"); // The directory that is uploaded to the previous directory +, that is, the current directory is pushed to the stack.
// System. Out. println (stack. Peek (). tostring ());}}}}