Должен ли я закрыть FileOutputStream, который заключен в PrintStream?

Я использую FileOutputStream с PrintStream следующим образом:

class PrintStreamDemo {  
    public static void main(String args[]) { 
        FileOutputStream out; 
        PrintStream ps; // declare a print stream object
        try {
            // Create a new file output stream
            out = new FileOutputStream("myfile.txt");

            // Connect print stream to the output stream
            ps = new PrintStream(out);

            ps.println ("This data is written to a file:");
            System.err.println ("Write successfully");
            ps.close();
        }
        catch (Exception e) {
            System.err.println ("Error in writing to file");
        }
    }
}

Я закрываю только PrintStream . Нужно ли мне также закрыть FileOutputStream ( out.close (); )?

18
задан Jon Schneider 24 December 2018 в 19:11
поделиться