Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.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_Command Line_Java.util.scanner_Java Io_File Io - Fatal编程技术网

如何从文本文件加载和执行java命令

如何从文本文件加载和执行java命令,java,command-line,java.util.scanner,java-io,file-io,Java,Command Line,Java.util.scanner,Java Io,File Io,我不熟悉Java编码,需要一些建议 我正在编写一个小程序,按照用户控制台输入的指定在图形面板中绘制形状。(使用scanner类) 例如,用户可以键入move 100 100将图形“笔”移动到x、y点, 或者可以键入line 100 200在两个坐标之间画一条线, 或者可以键入“circle 50”以绘制半径为50px的圆 我的下一个目标是包含命令'load example.txt',以加载一个文本文件(example.txt),其中包含一些命令,这些命令将在图形面板上执行 我被告知最好的方法是使

我不熟悉Java编码,需要一些建议

我正在编写一个小程序,按照用户控制台输入的指定在图形面板中绘制形状。(使用scanner类)

例如,用户可以键入
move 100 100
将图形“笔”移动到x、y点, 或者可以键入
line 100 200
在两个坐标之间画一条线, 或者可以键入“circle 50”以绘制半径为50px的圆

我的下一个目标是包含命令'load example.txt',以加载一个文本文件(example.txt),其中包含一些命令,这些命令将在图形面板上执行

我被告知最好的方法是使用

processCommandLine(String commandLine);
我已经在互联网上浏览了很长一段时间,现在正在寻找一些有用的信息,但到目前为止,我所能找到的只是如何从文本文件中读取,很多类似于以下内容:

 Scanner reader = new Scanner(new FileInputStream("example.txt");
我知道我需要使用Scanner类来读取文件内容,然后(我认为使用processCommandLine方法)在图形面板上执行它们

到目前为止,我的代码是:(我已经调用了保存在单独文件中的所有必要类和方法)

我需要文本文件包含,例如:

move 100 100 circle 50 line 100 200 移动100 圈50 行100 200 当我调用“load example.txt”操作时,应用程序将读取这个文本文件,并通过在图形画布上绘制指定的形状来执行其中的命令


任何帮助或指导都将不胜感激,我从事Java编程不到一年,一直在努力改进,因此欢迎任何建设性的批评。

因此,这是一个不错的项目,因为它向您介绍了代码重用和基本MVC(模型-视图-控制器)编程的概念。基本上,如果您认为图形上下文是您的视图,并且使用一组命令来控制对该视图的访问,那么您的模型显然是命令本身。您现在试图做的是使从其他来源提供模型成为可能,同时保持程序中的所有其他内容完好无损

因此,您的第一个任务是创建这个可重用的“控制器”代码,它知道如何获取模型(命令),并使用它影响视图(图形上下文)。您已经具备了所有必要的逻辑,只需将其移动到所需的功能中:

public static void processCommandLine(String[] commandArgs, GraphicsScreen graphics) {
   if (commandArgs == null || commandArgs.length = 0 || commandArgs[0] == null) {
      System.out.println("Null command!");
   }

   String command = commandArgs[0];

   if (command.trim().equalsIgnoreCase("move")) {
      int x = 0;
      int y = 0;

      x = Integer.parseInt(commandArgs[1]);
      y = Integer.parseInt(commandArgs[2]);

      graphics.moveTo(x, y);
   }

   else if (command.trim().equalsIgnoreCase("circle")) {
      int radius = Integer.parseInt(one[1]);
      graphics.circle(radius);

   }

   else if (command.trim().equalsIgnoreCase("line")) {
      int x = 0;
      int y = 0;

      x = Integer.parseInt(commandArgs[1]);
      y = Integer.parseInt(comamndArgs[2]);

      graphics.lineTo(x, y);
   }

   else if (command.trim().equalsIgnoreCase("clear")) {
      graphics.clear();
   }
   else {
      System.out.println("Invalid Command!");
   }
}
有了它,您可以将命令和图形屏幕传递给单个函数,它将“渲染”该命令。现在,您需要提供访问函数的不同方式。您的第一个访问方法是通过控制台向用户查询命令,然后执行它。但您也希望能够从一个文件中加载多个命令,并同时运行它们。因此,请定义一种从文件中读取一组命令的方法:

public static List<String> getCommands(String fileName) {
   if(fileName == null) return new ArrayList<String>(0);

   File file = new File(fileName);
   if(! (file.exists() && file.canRead()) {
      System.err.println("Cannot access file! Non-existent or read access restricted");
      return new ArrayList<String>(0);
   }

   List<String> commandLines = new ArrayList<String>(32);
   Scanner scanner = new Scanner(file);
   while(scanner.hasNextLine()) {
      commandLines.add(scanner.nextLine());
   }

   scanner.close();

   return commandLines;
}
公共静态列表getCommands(字符串文件名){
如果(fileName==null)返回新的ArrayList(0);
文件=新文件(文件名);
如果(!(file.exists()&&file.canRead()){
System.err.println(“无法访问文件!不存在或读取访问受限”);
返回新的ArrayList(0);
}
List命令行=新的ArrayList(32);
扫描仪=新扫描仪(文件);
while(scanner.hasNextLine()){
add(scanner.nextLine());
}
scanner.close();
返回命令行;
}
现在,您只需更改将命令管道化到渲染函数的方式,具体取决于命令源是控制台还是文件:

public final static void main(String[] args) {
   System.out.println("Let's draw something on the screen!");

   GraphicsScreen graphics = new GraphicsScreen();

   Scanner input = new Scanner(System.in); // used to read the keyboard

   String next; // stores the next line input
   String[] one;

   do {
      System.out.print("Enter a command (\"stop\") to finish : ");
      System.out.print("Type 'help' for a list of commands ");
      next = input.nextLine();
      one = next.split(" ");

      String command = one[0];

      if (command.trim().equalsIgnoreCase("help")) {
         System.out
               .println("Type 'move' followed by an X and Y co-ordinate to move the graphical pointer.");
         System.out
               .println("Type 'circle' followed by a radius value to output a circle.");
         System.out
               .println("Type 'line' followed by an X and Y co-ordinate to draw a line.");
         System.out.println("Type 'clear' to reset the graphical canvas.");
      }

      else if (command.trim().equalsIgnoreCase("load")) {
         List<String> commandLines = getCommands(one[1]);
         for (String commandLine : commandLines) {
            String[] commandArgs = commandLine.split(" ");
            processCommandLine(commandArgs, graphics);
         }
      }

      else if (command.trim().equalsIgnoreCase("stop")) {
         break;
      }

      else {
         processCommandLine(one, graphics);
      }
   } while (true);

   System.out
         .println("You have decided to stop entering commands. Program terminated!");
   graphics.close();
}
public最终静态void main(字符串[]args){
System.out.println(“让我们在屏幕上画点东西吧!”);
GraphicsScreen graphics=新的GraphicsScreen();
扫描仪输入=新扫描仪(System.in);//用于读取键盘
String next;//存储下一行输入
字符串[]1;
做{
System.out.print(“输入命令(\'stop\”)以完成:”;
System.out.print(“键入“help”以获取命令列表”);
next=input.nextLine();
一个=下一个。拆分(“”);
字符串命令=一个[0];
if(command.trim().equalsIgnoreCase(“帮助”)){
系统输出
.println(“键入'move',后跟X和Y坐标以移动图形指针”);
系统输出
.println(“键入'circle'后跟半径值以输出圆”);
系统输出
.println(“键入‘线’,后跟X和Y坐标以绘制线”);
System.out.println(“键入'clear'重置图形画布”);
}
else if(command.trim().equalsIgnoreCase(“加载”)){
List commandLines=getCommands(一个[1]);
for(字符串命令行:命令行){
字符串[]commandArgs=commandLine.split(“”);
processCommandLine(命令参数、图形);
}
}
else if(command.trim().equalsIgnoreCase(“停止”)){
打破
}
否则{
processCommandLine(一个,图形);
}
}虽然(正确);
系统输出
.println(“您已决定停止输入命令。程序已终止!”);
graphics.close();
}

这些(小调)更改后,您现在可以从多个不同来源获取命令,并将这些命令呈现到您的视图中。

有什么不起作用的吗?您是否有特定的问题?很难准确理解您的要求。我正在尝试创建“加载”命令,该命令将从文本文件加载命令,例如,如果e用户输入'load example.txt',然后在控制台上读取并执行文本文件中的命令。这些命令是graphics.draw命令,用于将用户输入的指定形状呈现到图形画布上。
public final static void main(String[] args) {
   System.out.println("Let's draw something on the screen!");

   GraphicsScreen graphics = new GraphicsScreen();

   Scanner input = new Scanner(System.in); // used to read the keyboard

   String next; // stores the next line input
   String[] one;

   do {
      System.out.print("Enter a command (\"stop\") to finish : ");
      System.out.print("Type 'help' for a list of commands ");
      next = input.nextLine();
      one = next.split(" ");

      String command = one[0];

      if (command.trim().equalsIgnoreCase("help")) {
         System.out
               .println("Type 'move' followed by an X and Y co-ordinate to move the graphical pointer.");
         System.out
               .println("Type 'circle' followed by a radius value to output a circle.");
         System.out
               .println("Type 'line' followed by an X and Y co-ordinate to draw a line.");
         System.out.println("Type 'clear' to reset the graphical canvas.");
      }

      else if (command.trim().equalsIgnoreCase("load")) {
         List<String> commandLines = getCommands(one[1]);
         for (String commandLine : commandLines) {
            String[] commandArgs = commandLine.split(" ");
            processCommandLine(commandArgs, graphics);
         }
      }

      else if (command.trim().equalsIgnoreCase("stop")) {
         break;
      }

      else {
         processCommandLine(one, graphics);
      }
   } while (true);

   System.out
         .println("You have decided to stop entering commands. Program terminated!");
   graphics.close();
}