1

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

4

1 回答 1

0

I would recommend to redirect the system output to a new one, use it, and restore the old one:

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");

Note that this comment differs from the one of @yohlulz in that the toString() method decodes de data. The charset argument of toString is optional. The parameterless one will decode the output with the platform's default character. ByteArrayOutputStream

于 2013-03-02T10:48:20.563 回答