将Outputstreams引导到System.Out Stream中

时间:2017-08-01 17:25:28

标签: java io stream

我试图使用将字符串写入另一个OutputStream的PrintStream(在本例中为System.out)

public void druckeDaten(OutputStream os){

    String t = "ID = " + getID() + " \"" + getTitel() + "\" "
            + "von " + interpreter + " aus " + getJahr()
            + " Spieldauer: " + dauer + " sek.";

    PrintStream pw = new PrintStream(os);
    pw.printf(t);
    pw.flush();
    pw.close();
}

如果我在循环中调用方法druckeDaten,控制台只打印一行

 Collections.sort(liste);
//liste is a LinkedList containing the Objects that I want to use the druckeDaten on
        for(Medium m : liste)
            m.druckeDaten(System.out);

我哪里出错?

1 个答案:

答案 0 :(得分:0)

您关闭了PrintStreamclose()会将System.out级联到基础流。您已关闭PrintStream(也是{{1}},因此无需将其换行。)