簡體   English   中英

DataOutputStream中的System.out未在控制台上打印

[英]System.out in DataOutputStream is not printing on the console

我正在使用DataOutputStream嘗試以下代碼。 傳遞給DataOutputStream的OutputStream不打印任何內容。 請看下面的代碼,請告訴我這段代碼有什么問題。

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);

}

}

你為什么使用數據輸出流? 你不能用Scanner讀取輸入嗎?

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

OutputStream基本上是一個二進制構造。 如果要編寫文本數據(例如從控制台),則應使用某些描述的Writer。 要將OutputStream轉換為Writer,請使用OutputStreamWriter 然后在Writer周圍創建一個PrintWriter ,您可以使用PrintWriter .println()讀取一行。 您可以替換以下行

DataOutputStream out = new DataOutputStream(out);

進入這個

PrintWriter out = new PrintWriter(new OutputStreamWriter(out));

然后可以在OutputStreamWriter的構造函數中顯式指定字符編碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM