title: How to display the contents of a .txt file on cmd window using Java?
body: <p>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 &quot;path&quot; variable contains the location where the demo.txt file is placed (as you can see obviously).</p>
  <pre><code>public static void main(String[] args){
      try{
          String path = &quot;C:\\Users\\Hp\\Documents\\NetBeansProject\\Project\\build\\classes\\&quot;;
          //cmd command to open open the txt file on cmd window
          String command = (&quot;type &quot; + path + &quot;\\demo.txt&quot;);
          //executing this command on cmd using java
          Process process = Runtime.getRuntime().exec(command);
          }catch(IOException e){
              e.printStackTrace();
          }
  </code></pre>
  <p>This code produces the following output:</p>
  <p><a href="https://i.stack.imgur.com/hbSKp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hbSKp.png" alt="The output on java console" /></a></p>
  <p>Don't mind the cringey or faulty code as I'm still a beginner in Java programming.</p>