RAII in Java… is resource disposal always so ugly?

后端 未结 5 837
日久生厌
日久生厌 2020-12-05 15:00

I just played with Java file system API, and came down with the following function, used to copy binary files. The original source came from the Web, but I added try/catch/f

5条回答
  •  不知归路
    2020-12-05 15:43

    For common IO tasks such as copying a file, code such as that shown above is reinventing the wheel. Unfortunately, the JDK doesn't provide any higher level utilities, but apache commons-io does.

    For example, FileUtils contains various utility methods for working with files and directories (including copying). On the other hand, if you really need to use the IO support in the JDK, IOUtils contains a set of closeQuietly() methods that close Readers, Writers, Streams, etc. without throwing exceptions.

提交回复
热议问题