2017-08-01 29 views
-1

我试图用一个PrintStream那字符串写入到另一个的OutputStream(在这种情况下下,System.out)指导Outputstreams到的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); 

我在哪里出错了?

+0

您正在关闭您的方法druckeDaten中的流。 – Markus

回答

0

您关闭了您的PrintStream,这会将close()级联到基础流。你关闭了System.out(这也是PrintStream,所以没必要包装它)。

+0

非常感谢 –