Igor
Igor

Reputation: 219

Java can't save Output Stream to a file

This code works fine on Computer A and doesn't work on Computer B ... I can't understand Why.. Nothing Exceptions or anything else ....On Computer A log file was created , on computer B log file was't created. Computer A and B have the same Java version... Do you have any ideas?

    String str = "cmd /C  dir tools>1.log";
    try {
        Runtime.getRuntime().exec(str);
    } catch (Exception e) {
        e.printStackTrace();
    }

PS this code works fine on both computers

    String str = "cmd /C  dir tools";

Upvotes: 0

Views: 540

Answers (2)

Farlan
Farlan

Reputation: 1900

You have to open the process' output stream to save the output to a file correctly.

You can do this by creating a Process object and saving that to a file:

Process p = Runtime.getRuntime().exec(str);
InputStreamReader reader = p.getInputStream();
BufferedReader buffer = new BufferedReader(reader);
String line = null;
while ((line = buffer.readLine() != null) {
  //write stuff to file here
}

Upvotes: 1

Envin
Envin

Reputation: 1523

Going on what SLaks said -- it is your best bet to use the built-in file APIs. Here is a link for a general tutorial

Using these APIs will take out any strange environment issues from Computer A to B...to C and so on.

Upvotes: 0

Related Questions