源於《java開發實戰經典》第12章12.8【例12.39】
import java.io.IOException;
import java.io.InputStream;import java.util.ArrayList;
import java.util.List;
public class AndrewKL12_39 {
public static void main(String[] args) throws IOException {
InputStream input = System.in ; // 從鍵盤接收數據
System.out.print("請輸入內容:") ; // 提示信息
int temp = 0 ; // 接收內容,字節字符單獨讀入流返回的都是數字.
List<Byte> allList = null ;//List只接收對象,不接收數據類型
allList=new ArrayList<Byte>();
while((temp=input.read())!=-1){
if((char)temp=='\n'){ // 退出循環,輸入回車表示輸入完成
break ;
}
allList.add((byte)temp) ;
}
Byte b[]=allList.toArray(new Byte[]{});
byte b1[]=new byte[b.length];
for(int i=0;i<b.length;i++) {
b1[i]=b[i];
}
input.close();
System.out.println("輸入的內容爲:" + new String(b1)) ;//將一個byte數組全部變成字符串
}
}