title: java get output of batch file command by command
body: <p>I need to execute <code>.bat</code> files in my java application. Suppose I have a text file with this sample content:</p>
  
  <pre><code>{
      "id": 12,
      "name": "test"
  }
  </code></pre>
  
  <p>And in my <code>.bat</code> file, I have a command for outputing text file content. So this is my <code>.bat</code> file content:</p>
  
  <pre><code>#some other commands
  more path\to\file.txt
  </code></pre>
  
  <p>And finally, this is my java code for executing this <code>.bat</code> file:</p>
  
  <pre><code>Process process = Runtime.getRuntime().exec("path\to\file.bat");
  BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
  List&lt;String&gt; outputs = new ArrayList&lt;&gt;();
  while ((line = reader.readLine()) != null) {
      outputs.add(line);
  }
  </code></pre>
  
  <p>After executing this code, the <code>outputs</code> list has data something like this:</p>
  
  <pre><code>[...,
  "{", 
  "    "id": 12",",
  "   "name": "test",",
  "}"
  ]
  </code></pre>
  
  <p>I means, this returns output <strong>line by line</strong>. But <strong>I want to have whole command output as one index of my list.</strong> In the other words, I want to have <strong>command by command instead of line by line output</strong>(every command has just one output).</p>
  
  <p>Is this possible doing something like that?</p>
  
  <p><strong>Edit:</strong> I tried using <code>ProcessBuilder</code> also, but result was the same.</p>