英文:
How to display the contents of a .txt file on cmd window using Java?
问题
我正在进行一个项目,想在CMD窗口上显示一个.txt文件的内容。我编写了以下代码来在cmd上打开demo.txt文件,但它不起作用。 "path" 变量包含了demo.txt文件所在的位置(如您可以明显看到的)。
public static void main(String[] args){
try{
String path = "C:\\Users\\Hp\\Documents\\NetBeansProject\\Project\\build\\classes\\";
//cmd command to open open the txt file on cmd window
String command = ("type " + path + "\\demo.txt");
//executing this command on cmd using java
Process process = Runtime.getRuntime().exec(command);
}catch(IOException e){
e.printStackTrace();
}
}
这段代码产生以下输出:
英文:
I am working on a project and I want to display the contents of a .txt file on the CMD window. I wrote this piece of code to open a demo.txt file on cmd but it does not work. The "path" variable contains the location where the demo.txt file is placed (as you can see obviously).
public static void main(String[] args){
try{
String path = "C:\\Users\\Hp\\Documents\\NetBeansProject\\Project\\build\\classes\\";
//cmd command to open open the txt file on cmd window
String command = ("type " + path + "\\demo.txt");
//executing this command on cmd using java
Process process = Runtime.getRuntime().exec(command);
}catch(IOException e){
e.printStackTrace();
}
This code produces the following output:
Don't mind the cringey or faulty code as I'm still a beginner in Java programming.
答案1
得分: 1
显示CMD窗口(正如您在问题中提到的)的可执行文件是:
C:\Windows\System32\conhost.exe
使用java.lang.ProcessBuilder
类启动conhost.exe
ProcessBuilder pb = new ProcessBuilder("conhost.exe");
Process proc = pb.start();
当您运行此Java代码时,将显示一个CMD窗口。
请注意,您无法在此窗口中键入命令,因为它的标准输入是您的Java程序而不是键盘。但是,您可以从您的Java代码向窗口发送命令。只需将命令写入Process
实例的输出流中。
首先获取Process
的输出流
OutputStream os = proc.getOutputStream();
然后将您需要的命令写入输出流。
我使用了[Windows]的start
命令打开了一个单独的窗口,您可以与之交互,并在该窗口中运行了您需要的命令。最后,我通过conhost.exe
关闭了打开的窗口。因此,通过start
命令打开的窗口仍然保持打开状态,而Java程序终止。
这是整个代码。
import java.io.IOException;
import java.io.OutputStream;
public class Script {
public static void main(String[] args) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("conhost.exe");
Process proc = pb.start(); // throws java.io.IOException
OutputStream os = proc.getOutputStream();
os.write("start /D C:\\Users\\Hp\\Documents\\NetBeansProject\\Project\\build\\classes type demo.txt".getBytes()); // throws java.io.IOException
os.write(System.lineSeparator().getBytes()); // throws java.io.IOException
os.write("exit".getBytes()); // throws java.io.IOException
os.write(System.lineSeparator().getBytes()); // throws java.io.IOException
os.flush(); // throws java.io.IOException
int status = proc.waitFor(); // throws java.lang.InterruptedException
System.out.println("Exit status = " + status);
}
}
英文:
The executable that displays a CMD window (as you refer to it in your question) is:
C:\Windows\System32\conhost.exe
Use class java.lang.ProcessBuilder
to launch conhost.exe
ProcessBuilder pb = new ProcessBuilder("conhost.exe");
Process proc = pb.start();
When you run this java code a CMD window will be displayed.
Note that you can't type commands into this window because its standard input is your java program and not the keyboard. However you can send commands to the window from your java code. You simply write to the output stream of the Process
instance.
First get the output stream of the Process
OutputStream os = proc.getOutputStream();
Then write your desired commands to the output stream.
I used the [Windows] start
command to open a separate window – which you can interact with – and ran your desired command in that window. And finally, I closed the window that I opened via conhost.exe
. As a result, the window opened by the start
command remains open and the java program terminates.
Here is the entire code.
import java.io.IOException;
import java.io.OutputStream;
public class Script {
public static void main(String[] args) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("conhost.exe");
Process proc = pb.start(); // throws java.io.IOException
OutputStream os = proc.getOutputStream();
os.write("start /D C:\\Users\\Hp\\Documents\\NetBeansProject\\Project\\build\\classes type demo.txt".getBytes()); // throws java.io.IOException
os.write(System.lineSeparator().getBytes()); // throws java.io.IOException
os.write("exit".getBytes()); // throws java.io.IOException
os.write(System.lineSeparator().getBytes()); // throws java.io.IOException
os.flush(); // throws java.io.IOException
int status = proc.waitFor(); // throws java.lang.InterruptedException
System.out.println("Exit status = " + status);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论