2014-03-30 86 views
3

我正在嘗試使用DataOutputStream的以下代碼。傳遞給DataOutputStream的OutputStream不會打印任何內容。請參閱我的下面的代碼,並請告訴我在此代碼中有任何錯誤。DataOutputStream中的System.out未在控制檯上打印

public class DataStreamsExample { 
public static void main(String[] args) throws IOException { 
    DataInputStream dis = new DataInputStream(System.in); 
    System.out.println("Enter the First Number"); 
    int i = dis.readInt(); 
    System.out.println("Enter the Second Numebr"); 
    int j= dis.readInt(); 
    int total = i+j; 
    DataOutputStream dos = new DataOutputStream(System.out); 
    dos.writeInt(total); 

} 

}

+0

DataInput顧名思義是用於二進制數據而不是文本。這意味着除非你真的知道你在做什麼,否則你將無法輸入二進制數。我建議你使用Scanner和PrintWriter等文本。 –

回答

2

你爲什麼要使用數據輸出流?你不能使用Scanner來讀取輸入嗎?

調用dos.flush()會打印出您的結果。

+0

我知道使用掃描儀。我的問題是爲什麼System.out不起作用。 –

+0

沖洗流。 – Arrem

+0

感謝它的工作..但不打印int。相反,字節打印我猜。 –