Java io ugly try-finally block

前端 未结 12 1031
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 05:57

Is there a not so ugly way of treat the close() exception to close both streams then:

    InputStream in = new FileInputStream(inputFileName);
          


        
相关标签:
12条回答
  • 2020-11-28 06:30

    In C#, there is using construction that closes closable objects automatically, when we leave the scope:

    using(Stream s = new Stream(filename)) {
      s.read();
    }
    

    I think that this is a short form for java's try-finally block. Java 6 has introduced the Closable interface. So, using is almost there. When the final step is done in Java 7, it will be terrific indeed.

    0 讨论(0)
  • 2020-11-28 06:31

    Since Java 7 there is a much nicer way to write try-finally block in regards to Closeable resources.

    Now you can create your resources in parenthesis after the try keyword, like this:

    try (initialize resources here) {
       ...
    }
    

    And they will be closed automatically after the block of code is finished. There is no need for the finally part.

    An example:

    try (
       ZipFile zf = new ZipFile(zipFileName);
       BufferedWriter writer = Files.newBufferedWriter(outputFilePath, charset);
    ) {
        // Enumerate each entry
        for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
            // Get the entry name and write it to the output file
            String newLine = System.getProperty("line.separator");
            String zipEntryName = ((java.util.zip.ZipEntry)entries.nextElement()).getName() + newLine;
            writer.write(zipEntryName, 0, zipEntryName.length());
        }
    }
    

    And after the for loop is done, the resources will be closed!

    0 讨论(0)
  • 2020-11-28 06:33

    In most cases the 'in' close() exception is irrelevant, so:

        try {
          copy(in, out);
        } finally {
        try {  in.close()  }  catch (Exception e) { /* perhaps log it */ }
        try {  out.close() }  catch (Exception e) {/* perhaps log it */ }
        } 
    

    It's usually bad practice to swallow exceptions, but in this case I think it's ok.

    0 讨论(0)
  • 2020-11-28 06:40

    I strongly believe that in Java 7.0, you do not need to explicitly close the stream yourself anymore. Language Features in Java 7

    try (BufferedReader br = new BufferedReader(new FileReader(path)) {
       return br.readLine();
    }
    
    0 讨论(0)
  • 2020-11-28 06:41
    try {
        final InputStream in = new FileInputStream(inputFileName);
        try {
            final OutputStream out = new FileOutputStream(outputFileName);    
            try {
                copy(in, out);
                out.flush(); // Doesn't actually do anything in this specific case.
            } finally {
                out.close();
            }
        } finally {
            in.close();
        }
    } catch (IOException exc) {
        throw new SomeRelevantException(exc);
    }
    

    Remember that opening a stream may throw an exception, so you do need a try between the stream openings (please don't do some hack involving nulls. Anything can throw an Error (which are not an instances of Exception).

    It turns out that catch and finally should rarely share the same try.

    Since Java SE 7 you can write use try-with-resource to avoid so much indentation. It more or less does the same thing although there are suppressed exception hidden away.

    try (
        final InputStream in = new FileInputStream(inputFileName);
        final OutputStream out = new FileOutputStream(outputFileName);    
    ) {
        copy(in, out);
        out.flush(); // Doesn't actually do anything in this specific case.
    } catch (IOException exc) {
        throw new SomeRelevantException(exc);
    }
    

    You may want to use the Execute Around idiom.

    I believe the standard good way to copy is using NIO's transferTo/transferFrom.

    0 讨论(0)
  • 2020-11-28 06:42

    Use

    IOUtils.closeNoThrow(myInputStream);

    Simple and elegant.

    0 讨论(0)
提交回复
热议问题