繁体   English   中英

将正在运行的Java程序的输出另存为String []

[英]Save output of running java program as String[]

我有以下将代码打印到控制台的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,但是由于缺乏此类的经验,我不知道如何将当前运行时分配给流程。 感谢您的任何建议。

我建议将系统输出重定向到一个新的输出,使用它并还原旧的输出:

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()方法解码数据。 toString的charset参数是可选的。 无参数的将使用平台的默认字符对输出进行解码。 ByteArrayOutputStream

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM