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;
}
有上面这样一个方法读取telnet的返回结果
如果不出现 --More-- 的控制符一切ok
但是有 --More-- 控制符的时候就卡那里不动了
知道要写个判断回调,关键是怎么写才能保存结果并顺利执行完
谢谢

解决方案 »

  1.   


    public String readUntilAll(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);
    // System.out.println(ch+"@"+lastChar);
    if(ch == lastChar) {
    if(sb.toString().endsWith(pattern)) {
    // System.err.println("!!!"+sb.toString());
    return sb.toString();
    }
    }
    if(sb.toString().endsWith("--More--")) {
    //sendAnyKey("\u0032");
    // write(" \r\n");
    write((char)32+"");
    }
    ch = (char)in.read();
    }
    }catch(Exception e) {
    e.printStackTrace();
    }
    return null;
    }
    改进了一下
    但是打印的结果如下
      62   0      10540        196      17348          0          0 DDP             
      63   0        196        196       7004          0          0 WLCCP MFP AP    
     --More--           64   0        360        196       7168          0          0 Dot1x Supplicant
     --More--          PID TTY  Allocated      Freed    Holding    Getbufs    Retbufs Process
      65   0        360        196       7168          0          0 Dot1x Supplicant
      66   0        360        196       7168          0          0 Dot1x Supplicant
    ……
      86   0        164          0       7168          0          0 Spanning Tree   
      88   0          0          0       7004          0          0 SNMP Timers     
     --More--           89   0       4608       3648      14012        167        167 IP SNMP         
     --More--          PID TTY  Allocated      Freed    Holding    Getbufs    Retbufs Process
      90   0          0          0      13004          0          0 PDU DISPATCHER  
      91   0          0          0      13004          0          0 SNMP ENGINE     怎么解决乱码?
    more后面的是退格符!
    谢谢
      

  2.   

    解决乱码你可以通过设置获取的String字符编码
      

  3.   


    public String readUntilAll(String pattern) {
    try {
    char lastChar = pattern.charAt(pattern.length() - 1);
    StringBuffer sb = new StringBuffer();
    char ch = (char)in.read();
    while(true) {
    // System.out.print(ch+"@"+(int)ch);
    sb.append(ch);
    if(ch == lastChar) {
    if(sb.toString().endsWith(pattern)){
    return sb.toString();
    }
    }
    if(sb.toString().endsWith("--More--")){
    write((char)32+"");
    }
    if (ch==8) {
    sb=sb.deleteCharAt(sb.length()-1);
    sb=sb.deleteCharAt(sb.length()-1);
    }
    ch = (char)in.read();
    }
    }catch(Exception e) {
    e.printStackTrace();
    }
    return null;
    }
    自己解决了
      

  4.   

    不谢谢你不行,国内外N多资料.都没找到解决 "--More--"相应乱码问题请教下,实际用的过程中,你还遇到过什么其他问题么?本人最近也做基于telnet登陆到网络设备的小项目谢谢