将运行java程序的输出保存为String []

时间:2013-03-02 09:50:58

标签: java

我有以下java代码将东西打印到控制台

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jmol.adapter.smarter.SmarterJmolAdapter;
import org.jmol.api.JmolViewer;
import org.jmol.util.Logger;
import org.openscience.jmol.app.jmolpanel.AppConsole;

public class Integrate {

    public static void main(String[] argv) throws IOException {

        JFrame frame = new JFrame("JMOL_WS_V1");
        frame.addWindowListener(new ApplicationCloser());
        frame.setSize(410, 700);
        Container contentPane = frame.getContentPane();
        JmolPanel jmolPanel = new JmolPanel();
        jmolPanel.setPreferredSize(new Dimension(400, 400));

        // main panel -- Jmol panel on top

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(jmolPanel);

        // main panel -- console panel on bottom

        JPanel panel2 = new JPanel();
        panel2.setLayout(new BorderLayout());
        panel2.setPreferredSize(new Dimension(400, 300));
        AppConsole console = new AppConsole(jmolPanel.viewer, panel2,
        "History State Clear");

        jmolPanel.viewer.setJmolCallbackListener(console);

        panel.add("South", panel2);

        contentPane.add(panel);
        frame.setVisible(true);

        //STARTUP SCRIPT

        String strError = jmolPanel.viewer.openFile("1644_____.pdb");
        if (strError == null){

                 //THIS IS THE COMMAND THAT CALCULATES STUFF
                jmolPanel.viewer.evalString("measure 3 4");
                jmolPanel.viewer.evalString("measure 2 4");
            }else{
                Logger.error(strError);
        }

    }

    static class ApplicationCloser extends WindowAdapter {
        @Override
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    }

    static class JmolPanel extends JPanel {

        JmolViewer viewer;

        private final Dimension currentSize = new Dimension();

        JmolPanel() {
            viewer = JmolViewer.allocateViewer(this, new SmarterJmolAdapter(), 
            null, null, null, null, null);
        }

        @Override
        public void paint(Graphics g) {
            getSize(currentSize);
            viewer.renderScreenImage(g, currentSize.width, currentSize.height);
        }
    }
}

它在控制台中打印出来:

(C) 2012 Jmol Development
Jmol Version: 13.0.12  2013-01-23 21:55
java.vendor: Apple Inc.
java.version: 1.6.0_41
os.name: Mac OS X
Access: ALL
memory: 10.8/85.0
processors available: 4
useCommandThread: false

FileManager.getAtomSetCollectionFromFile(1644_____.pdb)
FileManager opening /Users/juliofdiaz/Dropbox/CF/1644_____.pdb
The Resolver thinks Pdb
openFile(1644_____.pdb): 119 ms
reading 2602 atoms
ModelSet: haveSymmetry:false haveUnitcells:false haveFractionalCoord:false
1 model in this collection. Use getProperty "modelInfo" or getProperty "auxiliaryInfo" to inspect them.
Default Van der Waals type for model set to Jmol
2602 atoms created
Time to autoBond: 58 ms
ModelSet: autobonding; use  autobond=false  to not generate bonds automatically
Jmol 13.0.12  2013-01-23 21:55 DSSP analysis for model 1.1 - null

W. Kabsch and C. Sander, Biopolymers, vol 22, 1983, pp 2577-2637

We thank Wolfgang Kabsch and Chris Sander for writing the DSSP software,
and we thank the CMBI for maintaining it to the extent that it was easy to
re-engineer for our purposes. At this point in time, we make no guarantee
that this code gives precisely the same analysis as the code available via license
from CMBI at http://swift.cmbi.ru.nl/gv/dssp

All bioshapes have been deleted and must be regenerated.

measurement[0] = [[MET]1.CB #3, [MET]1.C #4, 0.219 nm]
measurement[1] = [[MET]1.CA #2, [MET]1.C #4, 0.143 nm]

我想在一个字符串数组中捕获此程序的所有输出(每个字符串项都是每一行)。我已经玩过为这个过程获取BufferedReader但是对我缺乏这个类的经验我不知道如何将当前的Runtime分配给一个进程。谢谢你的任何建议。

1 个答案:

答案 0 :(得分:0)

我建议将系统输出重定向到新的,使用它,然后恢复旧的输出:

PrintStream systemOut = System.out;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
System.setOut(ps);
System.out.println("Test string");
String content = baos.toString("ISO-8859-1");  // A charset
System.setOut(systemOut);
System.out.println("Restored");

请注意,此注释与@yohlulz的注释不同之处在于toString()方法对de data进行解码。 toString的charset参数是可选的。无参数的将使用平台的默认字符解码输出。 ByteArrayOutputStream