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-- 控制符的时候就卡那里不动了
知道要写个判断回调,关键是怎么写才能保存结果并顺利执行完
谢谢
解决方案 »
- 有什么工具可以分析java应用每个类每个函数的执行时间
- 求大神帮我把主要代码整理出来 发到我邮箱[email protected]谢谢
- * cannot be resolved to a type
- 我是java初学者,请推荐一本适合我看的书
- spring配置文件头的区别
- axis2 Rampart
- hibernate不支持union all该如何是好呀!
- 请教appfuse登录后为什么到mainMenu.jsp画面?
- dom,sax,jdom,jaxp,xerces有什么区别?现在项目要用,不知道选哪个,希望高手指点
- 周末,心情特别好,送分
- WebLogic的启动问题,求高手解决!谢谢
- 用JDOM生成XML文件,如何去掉版本信息
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后面的是退格符!
谢谢
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;
}
自己解决了