gpt4 book ai didi

java - 将控制台数据写入文件并在新框架上显示相同的文件

转载 作者:行者123 更新时间:2023-12-01 15:07:24 26 4
gpt4 key购买 nike

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

最佳答案

我想使用 PrintStream 将输出重定向到文本区域和 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);
}

};

另外,不要忘记将代码移至另一个线程!

关于java - 将控制台数据写入文件并在新框架上显示相同的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12800283/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com