⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filesystemshell.java

📁 用command模式写的一个简单的java 程序
💻 JAVA
字号:
/*******************************************************************************  Filename    :FileSystemShell.java
*  Description :The whole program's entry,which complete the initialization
*
*  Author      :xunzy
*
*  History     :2004-02-16   Created******************************************************************************/import java.io.*;import java.util.*; class FileSystemShell {      // Private variables  private static Hashtable COMMAND_TABLE = new Hashtable();  private static String  DEFAULTDIR  = "c:\\";  private static String  PROMPT = "[c:\\]";  private static String  VERSION = "1.0";  private static String  CURRENT_DIR="c:\\";  private static String  PARENT_DIR="c:\\";  private static File    CURRENT_FILE=new File(CURRENT_DIR);  private static boolean RUNNING = true;  // Shell operations  //private static void exit(int status) { System.exit(status); }      // Accessor methods  public static Hashtable getCommands() { return COMMAND_TABLE; }  public static String getCurrentDir() { return CURRENT_DIR; }  public static String getParentDir() { return PARENT_DIR; }  public static String getDefaultDir() { return DEFAULTDIR; }  public static String getPrompt() { return PROMPT; }  public static File getCurrentFile() { return CURRENT_FILE; }    public static void setCurrentDir(String dir)   {  	 CURRENT_DIR = dir;   	 setPrompt("["+dir+"]");  }  public static void setParentDir(String dir){PARENT_DIR = dir;}     public static void setPrompt(String prompt) { PROMPT = prompt; }  public static void setCurrentFile(File fl) { CURRENT_FILE = fl; }    // Executes a preinstantiated command we are sure is already  // present in the table  private static void execute(Command c, Vector v) {    if (c == null) {      System.out.println("No command was loaded; cannot execute the command.");      return;    }    try {      c.execute(CURRENT_FILE, v);    }    catch (CommandException ce) {      System.out.println(ce.getMessage());    }  }      // Another private method that enables us to specify a command  // by its string name and that loads the command first  private static void execute(String s, Vector v) {    execute(loadCommand(s), v);  }              // Loads the command specified in commandName;   public static Command loadCommand(String commandName) {    // The method returns a null command unless some of its     // internal logic assigns a new reference to it    Command theCommand = null;            // First see if the command is already present in the hashtable    if (COMMAND_TABLE.containsKey(commandName)) {      theCommand = (Command)COMMAND_TABLE.get(commandName);      return theCommand;    }    try {      // Here we use a little introspection to see if a class      // implements Command before we instantiate it      Class commandInterface = Class.forName("Command");      Class commandClass = Class.forName(commandName);                  // Check to see if the class is assignable from Command       // and if so, put the instance in the command table      if (!(commandInterface.isAssignableFrom(commandClass)))         System.out.println("[" + commandName + "]: Not a command");      else {        theCommand = (Command)commandClass.newInstance();        COMMAND_TABLE.put(commandName, theCommand);	return theCommand;      }    }    catch (ClassNotFoundException cnfe) {      System.out.println("[" + commandName + "]: command not found");    }    catch (IllegalAccessException iae) {      System.out.println("[" + commandName + "]: illegal access");    }    catch (InstantiationException ie) {      System.out.println("["+commandName+"]: command couldn't be instantiated");    }    finally {      return theCommand;          // theCommand is null if we get here    }  }      // This method reads a line of input, gets the command and arguments  // within the line of input, and then dynamically loads the command  // from the current directory of the running shell  private static void readInput() {    // Get the input from System.in    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));              // Begin reading input    try {      while (RUNNING) {        System.out.print(PROMPT + "% ");                        // Tokenize the line, read each token, and pass the token        // into a convenient remaining arguments Vector that we        // pass into the Command        StringTokenizer tokenizer = new StringTokenizer(br.readLine());        Vector remainingArgs = new Vector();        String commandToken = "";        if (tokenizer.hasMoreTokens()) {          commandToken = tokenizer.nextToken();          while (tokenizer.hasMoreTokens())            remainingArgs.addElement(tokenizer.nextToken());        }                        // Dynamically load the class for the appropriate command        // based upon the case-sensitive name of the first token,        // which is the command token        if (!(commandToken.equals("")))           execute(commandToken, remainingArgs);      }    }    catch (java.io.IOException ioe) {      System.out.println("Caught an IO exception reading a line of input");    }  }  // Constructor  FileSystemShell(String[] args) {  }      // Main method that reads input until the user exits  public static void main(String[] args) {    FileSystemShell shell = new FileSystemShell(args);    System.out.println("******************************************");    System.out.println("*                                        *");    System.out.println("*    Welcome to our File System(V1.00)   *");    System.out.println("*                                        *");    System.out.println("******************************************");    shell.readInput();  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -