这是我发现的,但在这段代码中它会读取您输入的内容,而我不希望这样
我正在做一个名为 Knight's Tour 的程序,我在命令提示符下得到输出。我想做的就是从命令提示符中读取行并将其存储在名为 Knight.txt 的输出文件中。谁能帮我吗。谢谢。
try
{
//create a buffered reader that connects to the console, we use it so we can read lines
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//read a line from the console
String lineFromInput = in.readLine();
//create an print writer for writing to a file
PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
//output to the file a line
out.println(lineFromInput);
//close the file (VERY IMPORTANT!)
out.close();
}
catch(IOException e)
{
System.out.println("Error during reading/writing");
}
最佳答案
您不需要 Java。只需将游戏的输出重定向到文件即可:
game > knight.txt
关于java - 如何使用 Java 语言从命令提示符获取输出并从中创建文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12488312/