如何使用Java在cmd窗口中显示.txt文件的内容? [英] How to display the contents of a .txt file on cmd window using Java?
问题描述
我正在一个项目上,我想在CMD窗口上显示.txt文件的内容.我编写了这段代码,以在cmd上打开demo.txt文件,但是它不起作用. 路径"指的是路径".变量包含demo.txt文件的放置位置(如您所见).
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();
}
此代码产生以下输出:
因为我还是Java编程的初学者,所以不要介意凌乱或错误的代码.
Don't mind the cringey or faulty code as I'm still a beginner in Java programming.
推荐答案
显示 CMD窗口(如您在问题中所指)的可执行文件为:
The executable that displays a CMD window (as you refer to it in your question) is:
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
实例的输出流.
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.
首先获取Process
OutputStream os = proc.getOutputStream();
然后将所需的命令写入输出流.
Then write your desired commands to the output stream.
我使用了[Windows] start
命令打开一个单独的窗口(可以与之交互),并在该窗口中运行所需的命令.最后,我关闭了通过conhost.exe
打开的窗口.结果,由start
命令打开的窗口保持打开状态,并且Java程序终止.
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.
这是完整的代码.
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);
}
}
这篇关于如何使用Java在cmd窗口中显示.txt文件的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!