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);
}
}`

I am trying to redirect the console output into a file and then fetching that file and displaying it in a new frame.. output is writing into the file but displaying it in a frame through JtextArea is not working.. please check the code and help me out.

  • 1
    Why don't you write directly to both the file and the textarea? – Guillaume Polet Oct 09 '12 at 12:47
  • 1
    Oh, and btw, you are performing this code upon ActionEvent which means that you are running this code on the EDT (Event dispatching thread) which will prevent all repaint events from occuring and your UI will look frozen until the code ends. Consider moving the code in another Thread, for example by using SwingWorker. – Guillaume Polet Oct 09 '12 at 12:51
  • Perhaps an IOException while reading the File? `System.out.println(e);` doesnt work here without setting the `orgStream` back to `System.setOut()` – oliholz Oct 09 '12 at 13:05
  • thank you both.. writing directly to both file and textarea is a good idea.. can u please give me sample code.. @Guillaume Polet – user1569620 Oct 09 '12 at 13:16
  • why i shouldnt use IOException here ?? i didn get the reason.. @oliholz – user1569620 Oct 09 '12 at 13:17
  • not using IOException, perhaps it is undetected thrown. – oliholz Oct 09 '12 at 14:22
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Oct 10 '12 at 01:20

1 Answers1

1

I guess with a PrintStream redirecting both output to the textarea and the FileOutputStream, this should work ok. Here is a snippet that should get you started (but I haven't tested it):

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

    };

Also don't forget to move your code to another Thread!

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117