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