我还在学习java,所以请耐心等待。我有一个程序,如果你键入一个关键词,它将运行一个方法和东西。我正在尝试做的是创建一个可以随时学习的过程,IE是一个外部命令列表,当它输入时它将像原始关键字一样运行。
这可能没什么意义,所以这里是我想要发生的一个例子:
程序启动,我键入关键字1(为了参数的缘故,说删除),它运行方法。 现在我想输入一个像'删除'这样的关键字,它应该和'删除'一样。由于代码中不存在“删除”,因此我输入“新命令:删除运行删除”或类似内容。在外部文件中,它会创建一行或类似“删除=删除”的内容。现在,当我输入Remove it检查列表文件时,看到remove与delete相同。
对不起,如果这没有多大意义。
就像我说的那样,我仍然处于java的学习过程中,所以对解释的任何帮助都会很棒!
答案 0 :(得分: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");
}
}
}
答案 1 :(得分: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();
}
}