1

我还在学习java,所以请多多包涵。我有一个程序,如果您在其中键入关键字,它将运行一个方法和东西。我正在尝试做的是创建一个可以随时学习的过程,即一个外部命令列表,当输入它时它会像原来的关键字一样运行。

这可能没有多大意义,所以这是我想要发生的一个例子:

程序启动,我输入关键字 1(为了参数,让我们说删除),它运行该方法。现在我想输入一个像“删除”这样的关键字,它的作用应该与“删除”相同。由于代码中还没有“删除”,因此我输入了类似于“新命令:删除运行删除”或类似内容的内容。在外部文件中,它会生成一行或类似“删除 = 删除”的内容。现在,当我输入 Remove 时,它​​会检查列表文件并看到 remove 与 delete 相同。

对不起,如果这没有多大意义。

就像我说的那样,我仍在学习 java 的过程中,所以任何关于解释的帮助都会很棒!

4

2 回答 2

0

编辑 我很确定这几乎正是你想要的,我把它美化了,因为我上次对自己的错误感到难过。这个甚至写出了用户输入的新命令。我错过了什么?

import java.io.*;
import java.util.ArrayList;

public class test {

  public static void main (String[] args) {
    test t = new test();
    t.test();
  }

  /** A test method for our code **/
  public void test(){
    String[] command = new String[2];

      BufferedReader commands = null;

      //initialize list of commands
      ArrayList<String[]> commandList = new ArrayList<String[]>();

      //Get a list of Commands from a file
      this.getCommands( commandList );

      //get the next command
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Next Thing?\n");
      this.getCommand( br, command, commandList );
      if ( command[1] == null ){
        System.out.print("New command?\n");
        commandList.add( this.makeCommand( br, command ) );
      }
      else{
        //Not sure what you want to do here, this is if the method IS found
        System.out.println( "We found: "+command[1]);
      }

      this.save(commandList);  
  }

  /**Returns the old list of commands**/
  public void getCommands( ArrayList<String[]> commandList ){
    BufferedReader commands;
    try{
      String sCurrentLine;

      commands = new BufferedReader(new FileReader("testing.txt"));

      while ((sCurrentLine = commands.readLine()) != null) {
        commandList.add( sCurrentLine.split(":") );
      }

    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  /**
  Asks the user for a command and checks it against known commands. It's not a 
  very efficient algorithm, but effective. 
  **/
  public void getCommand( BufferedReader br, String[] command, 
                          ArrayList<String[]> commandList ){

    try {
      command[0] = br.readLine();
      for ( String[] com : commandList ){
        if (com[0].equals( command[0] )){
          command[1] = com[1];
        }
      }
    } 
    catch (IOException ioe) 
      {
       System.out.println("IO error trying to read your commnad!");
      }
  }

  /** Makes a new command, to be used when one isn't known **/
  public String[] makeCommand( BufferedReader br, String[] command ){
    try{
      command[1] = br.readLine();
    }
    catch( IOException ioe)
      {
        System.out.println("Oh no!!!");
      }
      return command;
  }

  /** Saves your stuff **/
  public void save( ArrayList<String[]> commandList){
    try{
      PrintWriter writer = new PrintWriter( "testing.txt","UTF-8" );
      for ( String[] com : commandList ){
        writer.println( com[0]+":"+com[1] );
      }
      writer.close();
    }
    catch( Exception ioe ){
      System.out.println("You're in trouble");
    }
  }


}
于 2013-07-31T19:10:05.403 回答
0

java.util.Properties看起来像你需要的。假设您有一个文件包含将“删除”映射到“删除”的单行,如下所示:

remove = delete

您将使用以下代码创建一个 Properties 实例并将文件读入其内部 Hashtable 然后重新存储它:

import java.util.*;
import java.io.*;

public class Example
{
    public static void main(String[] args)
    {
        Properties mappings = new Properties();
        BufferedInputStream inStream = new BufferedInputStream(new FileInputStream("path/to/mappings.txt"));
        mappings.load(stream);
        stream.close();
        System.out.println("mappings.getProperty(\"remove\") should return \"delete\":" + mappings.getProperty("remove");
        System.out.println("Mapping \"quit\" to \"exit\"...");
        mappings.setProperty("quit", "exit");
        System.out.println("Saving new mappings...");
        BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream("path/to/mappings.txt");
        mappings.store(outStream, "Command aliases for <program name>");
        outStream.close();
    }
}
于 2013-07-31T19:53:59.507 回答