Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中为关键字引用外部文件_Java_File Io_External_Keyword - Fatal编程技术网

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

如何在java中为关键字引用外部文件,java,file-io,external,keyword,Java,File Io,External,Keyword,我仍在学习java,所以请容忍我。我有一个程序,如果你在其中键入一个关键字,它将运行一个方法和东西。我想做的是创建一个可以随过程学习的过程,即一个外部命令列表,当输入它时,它会像原始关键字一样运行 这可能没有多大意义,因此下面是我希望发生的一个例子: 程序启动时,我输入关键字1以便参数让我们说删除,它运行方法。 现在我想输入一个像“Remove”这样的关键字,该关键字的作用应该与“Delete”相同。因为“Remove”不在代码中,所以我输入了一些类似于新命令的内容:Remove runs De

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

这可能没有多大意义,因此下面是我希望发生的一个例子:

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

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

就像我说的,我仍在学习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.util.Properties看起来像您需要的。假设您有一个包含一行的文件,该行将“remove”映射为“delete”,如下所示:

remove = delete
您可以使用以下代码创建属性实例,并将文件读入其内部哈希表,然后重新存储它:

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

考虑使用一个数据库而不是一个平面文件。如果你已经尝试过这个问题,它可能值得张贴你的代码。嗯,问题是我不太确定从哪里开始。所以我所有的代码都是用于Delete和stuff的基本方法。也许可以从谷歌搜索反射开始。要以字符串形式运行方法,您很可能需要了解反射或类。您可以在这里阅读如何在Java中写入和读取文件:感谢您的帮助,但每当我尝试此操作时,总是会收到一个无法找到或加载主类Text.Java的错误消息。我可能错过了一些东西,不是吗?我发现了,但现在它总是返回,这是未知的。你是怎么建立你的命令文件的?你是对的,但为我辩护,我是个白痴。“==”操作符检查引用,如:这两个对象在内存中是同一个对象吗。我的意思是,这两件事和单词一样吗,用equals函数检查。对不起!我最近一直在使用Python,它不是一个问题。。。