0
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) throws IOException
{
String[] arg;
Runtime rt = Runtime.getRuntime();
System.out.println ("Connecting to router");
System.out.println(n1);
System.out.println(n2); 
jButton2.setText("Connecting");

File file = new File("out1.txt");
BufferedReader reader = null;

try

{
JSch jsch=new JSch();
String host=null;
String user= "pritam";
host = "172.16.12.1";
Session session=jsch.getSession(user, host, 22);
session.setPassword("passwrd");


ByteArrayInputStream bi = new ByteArrayInputStream("ssh -c ?\n".getBytes());                         
session.setUserInfo(ui);
session.connect(30000);   // making a connection with timeout.
Channel channel=session.openChannel("shell");                    
channel.setInputStream(bi);
channel.connect(3*1000);                          
// Saving the orginal stream
orgStream = System.out;
fileStream = new PrintStream(new FileOutputStream("out1.txt",true));     
// Redirecting console output to file
System.setOut(fileStream);
// Redirecting runtime exceptions to file
channel.setOutputStream(System.out);
System.setErr(fileStream);


String storeAllString = "";
JFrame frame= new JFrame(" Security Features");
frame.setSize(550,600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel=new JPanel();

JTextArea jt= new JTextArea(50,50);
JTextArea textArea=new JTextArea(storeAllString);
textArea.setLineWrap(true);
frame.add(panel);
panel.add(jt);                         
JScrollPane scrollBarForTextArea=new   JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
frame.add(scrollBarForTextArea);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

jt.setEditable(false);


String filePath = "/home/balki/Desktop/Desktop files/ScanModule/out1.txt";

BufferedReader r = new BufferedReader( new FileReader( filePath ) );

String line = r.readLine();

while ( line!=null )
{
storeAllString += line;
line = r.readLine();
}

r.close();

jt.setText( storeAllString );
jt.setVisible(true);

}

catch(Exception e)
{
  System.out.println(e);
}
}`

我正在尝试将控制台输出重定向到一个文件中,然后获取该文件并将其显示在一个新框架中。输出正在写入文件,但通过 JtextArea 在框架中显示它不起作用..请检查代码并帮助我出去。

4

1 回答 1

1

我猜想使用 PrintStream 将输出都重定向到 textarea 和 FileOutputStream,这应该可以。这是一个可以帮助您入门的片段(但我尚未对其进行测试):

    final FileOutputStream fos = new FileOutputStream("/home/balki/Desktop/Desktop files/ScanModule/out1.txt");
    final JTextArea textArea = new JTextArea();
    final PrintStream ps = new PrintStream(fos) {

        private Formatter formatter;

        @Override
        public void write(int b) {
            textArea.append(String.valueOf((char) b));
            super.write(b);
        }

        @Override
        public void write(byte[] buf, int off, int len) {
            textArea.append(new String(buf, off, len));
            super.write(buf, off, len);
        }

        @Override
        public void print(boolean b) {
            textArea.append(String.valueOf(b));
            super.print(b);
        }

        @Override
        public void print(char c) {
            textArea.append(String.valueOf(c));
            super.print(c);
        }

        @Override
        public void print(int i) {
            textArea.append(String.valueOf((char) i));
            super.print(i);
        }

        @Override
        public void print(long l) {
            textArea.append(String.valueOf(l));
            super.print(l);
        }

        @Override
        public void print(float f) {
            textArea.append(String.valueOf(f));
            super.print(f);
        }

        @Override
        public void print(double d) {
            textArea.append(String.valueOf(d));
            super.print(d);
        }

        @Override
        public void print(char[] s) {
            textArea.append(new String(s));
            super.print(s);
        }

        @Override
        public void print(String s) {
            textArea.append(s);
            super.print(s);
        }

        @Override
        public void print(Object obj) {
            textArea.append(String.valueOf(obj));
            super.print(obj);
        }

        @Override
        public void println() {
            textArea.append(System.getProperty("line.separator"));
            super.println();
        }

        @Override
        public void println(boolean x) {
            print(x);
            println();
        }

        @Override
        public void println(char x) {
            print(x);
            println();
        }

        @Override
        public void println(int x) {
            print(x);
            println();
        }

        @Override
        public void println(long x) {
            print(x);
            println();
        }

        @Override
        public void println(float x) {
            print(x);
            println();
        }

        @Override
        public void println(double x) {
            print(x);
            println();
        }

        @Override
        public void println(char[] x) {
            print(x);
            println();
        }

        @Override
        public void println(String x) {
            print(x);
            println();
        }

        @Override
        public void println(Object x) {
            print(x);
            println();
        }

        @Override
        public PrintStream printf(Locale l, String format, Object... args) {
            // TODO Auto-generated method stub
            return super.printf(l, format, args);
        }

        @Override
        public PrintStream format(String format, Object... args) {
            synchronized (this) {
                if (formatter == null || formatter.locale() != Locale.getDefault()) {
                    formatter = new Formatter((Appendable) textArea);
                }
                formatter.format(Locale.getDefault(), format, args);
            }
            return super.format(format, args);
        }

        @Override
        public PrintStream format(Locale l, String format, Object... args) {
            synchronized (this) {
                if (formatter == null || formatter.locale() != l) {
                    formatter = new Formatter((Appendable) textArea, l);
                }
                formatter.format(l, format, args);
            }
            return super.format(l, format, args);
        }

        @Override
        public PrintStream append(CharSequence csq, int start, int end) {
            CharSequence cs = csq == null ? "null" : csq;
            textArea.append(cs.subSequence(start, end).toString());
            return super.append(csq, start, end);
        }

    };

另外不要忘记将您的代码移动到另一个线程!

于 2012-10-09T13:56:15.937 回答