Outputstream write only when I close Eclipse Application

时间:2015-11-12 11:32:12

标签: java eclipse-plugin outputstream processbuilder

Goodmorning guys,

I'm trying to develop an Eclipse Plugin that run an external program with ProcessBulder.

During the exectution, I try to write the output on disk, but the plugin doesn't write anything until I close the execution of Eclipse Application.

public void run() 
    {
        ProcessBuilder pb = new ProcessBuilder("NuSMV.exe","-int");
        Process process = null;
        try {
            process = pb.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        OutputStream out = process.getOutputStream();
        // Write commands
        PrintWriter commands = new PrintWriter(out);        
        commands.println("reset");
        commands.println("set default_trace_plugin 4");             
        commands.println("read_model -i C:\\temp/ascensore.smv");               
        commands.println("go");
        commands.println("check_ctlspec");
        commands.println("show_traces -o C:\\temp/showtraces.xml");     
        commands.close();
        process.getOutputStream().close();

    }   

Showtraces.xml is written after the end of eclipse. How Can I have the output before this closing?

4 个答案:

答案 0 :(得分:0)

First flush the printwriter then close it.

ie

commands.flush();

Refer. http://docs.oracle.com/javase/8/docs/api/java/io/PrintWriter.html#flush--

答案 1 :(得分:0)

在调用close()方法之前,我已经尝试过使用commands.flush(),但这不会改变任何内容。

相反,如果我编写多个文件,例如file-1.xml,file-2.xml,file-3.xml,则在关闭应用程序和文件之前编写文件数字1和2第三号是在关闭后写的。

答案 2 :(得分:0)

我不确定它会有所帮助,但试图改变这个:

commands.close();
process.getOutputStream().close();

对此:

commands.flush();
commands.close();
out.flush();
out.close();

答案 3 :(得分:0)

使用其他构造函数设置autoflush模式(see Javadoc)。

PrintWriter commands = new PrintWriter(out, true);

更改此选项可让您以最小的更改保留代码。

每次拨打printlnprintfformat都会自动刷新缓冲区,解决问题。