Wednesday, February 02, 2005

Methods I cannot do without

In almost any project I work on I wind up writing certain methods over and over again. High among them are:

public static void copyAndClose(final InputStream in, final OutputStream out)
        throws IOException {
    try {
        // CopyUtils is from Jakarta IO
        CopyUtils.copy(in, out);
        out.flush();

    } finally {
        try {
            out.close();

        } finally {
            in.close();
        }
    }
}

public static File createTemporary(final String prefix) {
    final File tmp = File.createTempFile(prefix, null);

    tmp.deleteOnExit();

    return tmp;
}

Perhaps I can persuade Jakarta IO to take on copyAndClose, but there seems low odds of the JDK including createTemporary.

Post a Comment