java - 如何在java中引用外部文件的关键字

标签 java file-io external keyword

我还在学习java,所以请耐心等待。我有一个程序,如果你在其中输入一个关键字,它就会运行一个方法和东西。我想做的是创建一个可以随时学习的过程,即外部命令列表,当输入该命令时,它将像原始关键字一样运行。

这可能没有多大意义,所以这是我想要发生的事情的示例:

程序启动,我输入关键字 1(为了便于论证,我们可以说删除),它运行该方法。 现在我想输入“删除”这样的关键字,它的作用应该与“删除”相同。由于代码中尚未包含“删除”,因此我输入了类似于“新命令:删除运行删除”或类似内容的内容。在外部文件中,它会生成一行或类似“删除=删除”之类的内容。现在,当我输入“删除”时,它会检查列表文件,并发现“删除”与“删除”相同。

抱歉,如果这没有多大意义。

就像我说的,我仍然处于 java 的学习过程中,所以任何解释方面的帮助都会很棒!

最佳答案

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

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");
    }
  }


}

关于java - 如何在java中引用外部文件的关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17978205/

相关文章:

java - 自动查找和裁剪相关图像区域

python - 你如何检查文件何时在 Python 中完成复制?

performance - Delphi 中的文本文件写入性能

hyperlink - 如何在 Angular 2 中创建指向外部 URL 的链接

php - 在 CakePHP 2 中执行外部 PHP 脚本

java - Wicket Auth 角色入门

java - 似乎无法将 int 转换为字符串并运行我的程序,因此它输出 330

java - 当文件是目录时,文件 isDirectory() 给出 false

c# - 用于在独立线程中同时读取/写入同一文件的不同部分的多个 FileStreams

request - 我们检测到您的应用在一个或多个应用或 APK 包的 list 文件中包含请求旧外部存储标记