将控制台数据写入文件并在新帧上显示相同的文件

时间:2012-10-09 12:41:17

标签: java swing file awt jtextarea

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将其显示在一个框架中不起作用..请检查代码并帮助我。

1 个答案:

答案 0 :(得分: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);
        }

    };

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