第一个问题,看这个例子:
try {
Process myPro = Runtime.getRuntime().exec("C:/dir.bat");
BufferedInputStream bis = new BufferedInputStream(myPro.getInputStream());
byte[] aByte = new byte[1024];
int len = 0;
while(len != -1){
len = bis.read(aByte);
System.out.write(aByte,0,len);
}
}
catch (Exception ex) {
}第二个问题,直接遍历就是啊,有什么问题?
try {
Process myPro = Runtime.getRuntime().exec("C:/dir.bat");
BufferedInputStream bis = new BufferedInputStream(myPro.getInputStream());
byte[] aByte = new byte[1024];
int len = 0;
while(len != -1){
len = bis.read(aByte);
System.out.write(aByte,0,len);
}
}
catch (Exception ex) {
}第二个问题,直接遍历就是啊,有什么问题?
将System.out.write(aByte,0,len);
改为System.out.write(aByte);更好,因为最后一次循环取得的aByte的长度并不一定为1024。
问题解决了吗?怎么解决的?
正因为最后一次循环取得的aByte的长度并不一定为1024,所以才只能用:System.out.write(aByte,0,len);
我在Win2000中文版+JBuilder5下调了一下,暂时还没有发现什么问题,不知你是否有更好的办法?以下是源代码:
package length;import java.io.*;
import java.util.*;/**
* Title: 获取用Runtime.getRuntime().exec()方法启动的进程向System.out的
* 输出中的全角字符
* Description:
* Copyright: Copyright (c) 2001
* Company: Powerise
* @author Dino Santiago
* @version 1.0
*/public class OutStream { public OutStream() {
Process myPro;
BufferedInputStream bis;
byte[] aByte;
//存储全角字符的向量
Vector multiArray;
String str;
String tmpStr; try {
int len = 0;
myPro = Runtime.getRuntime().exec("C:/dir.bat");
bis = new BufferedInputStream(myPro.getInputStream());
aByte = new byte[1024];
multiArray = new Vector(); while (len != -1) {
len = bis.read(aByte);
str = new String(aByte); for (int i = 0; i < str.length(); i++) {
tmpStr = String.valueOf(str.charAt(i)); //若一个String的getBytes().lengh为2,那么它就是全角的(双字节)
if (tmpStr.getBytes().length == 2) {
multiArray.add(tmpStr);
}
} System.out.write(aByte);
} for (int i = 0; i < multiArray.size(); i++) {
System.out.println((String)multiArray.get(i));
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
myPro = null;
bis = null;
aByte = null;
multiArray = null;
str = null;
tmpStr = null;
}
} public static void main(String[] args) {
OutStream outStream1 = new OutStream();
}
}