read()把字节读取成字符,按字符进行读取输出流,
readline()按行读取,直到读到空格或者换行符停止。
这俩可以互换使用,readline速度略慢,直接写两个使用的例子吧:
//本地调用命令行时,按字符读取
Runtime run = Runtime.getRuntime ();
Process process = run.exec ( cmd );
InputStream in = process.getInputStream ();
StringBuffer out = new StringBuffer ();
byte[] b = new byte[8192];
for (int n; (n = in.read ( b )) != -1; ) {
out.append ( new String ( b, 0, n ) );
}
System.out.println ( out.toString () );
in.close ();
process.destroy ();
//远程调用linux时,按行读取
InputStream stdout = new StreamGobbler ( session.getStdout () );
BufferedReader br = new BufferedReader ( new InputStreamReader ( stdout ) );
String line;
String result=null;
while ((line = br.readLine ()) != null) {
if (line.contains ( "copied" )) {
result = "yes";
break;
} else
result = null;
}