Java 保存telnet的输出

Java 保存telnet的输出,java,string,telnet,Java,String,Telnet,我已经用java创建了telnet,但是我不知道如何将输出保存到文件中。。 你能帮我吗?? 这是我的密码 这是用于连接到目标的 public class telnetsample { private static TelnetClient telnet = new TelnetClient(); private InputStream in; private PrintStream out; private char prompt =

我已经用java创建了telnet,但是我不知道如何将输出保存到文件中。。 你能帮我吗?? 这是我的密码

这是用于连接到目标的

     public class telnetsample
     {
     private static TelnetClient telnet = new TelnetClient();
     private InputStream in;
     private PrintStream out;
     private char prompt = '$';

     public telnetsample( String server, String username, String password , String command) {
     try {
 // Connect to the specified server
 telnet.connect( server, 23 );

 // Get input and output stream references
 in = telnet.getInputStream();
 out = new PrintStream( telnet.getOutputStream() );

 // Log the user on
 readUntil( "Username: " );
 write( username );
 readUntil( "Password: " );
 write( password );
     readUntil ("hostname");
     write (command);


 // Advance to a prompt
 readUntil( prompt + " " );
}
catch( Exception e ) {
 e.printStackTrace();
}
 }
这用于读取终端上的命令

    public String readUntil( String pattern ) {
  try {
 char lastChar = pattern.charAt( pattern.length() - 1 );
 StringBuffer sb = new StringBuffer();
 boolean found = false;
 char ch = ( char )in.read();
 while( true ) {
  System.out.print( ch );
  sb.append( ch );
  if( ch == lastChar ) {
    if( sb.toString().endsWith( pattern ) ) {
     return sb.toString();
    }
  }
  ch = ( char )in.read();
 }
 }
 catch( Exception e ) {
 e.printStackTrace();
 }
 return null;
}
发送命令

    public void write( String value ) {
 try {
 out.println( value );
 out.flush();
 System.out.println( value );

 }
 catch( Exception e ) {
 e.printStackTrace();
 }
 }
用于插入用户名等

       public static void main( String[] args ) {


   try {
 telnetsample telnet = new telnetsample( "ip", 
                     "user", 
                     "password",
             "command");


   }
   catch( Exception e ) {
 e.printStackTrace();
   }

   System.exit(0);
   Runtime.getRuntime().exit(0);

  }
 }
  • 在构造函数中创建一个
  • 在代码中添加一行写入输出流的代码,该行就是您执行的
    System.out.println()
  • 代码中两个出口(0)有什么用途?