如何使用Java在cmd窗口中显示.txt文件的内容?

huangapple go评论120阅读模式
英文:

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:

如何使用Java在cmd窗口中显示.txt文件的内容?

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);
    }
}

huangapple
  • 本文由 发表于 2020年8月4日 18:03:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63244536.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定