public class IOUtils
extends java.lang.Object
| 限定符和类型 | 方法和说明 |
|---|---|
static void |
close(java.net.URLConnection conn)
Closes a URLConnection.
|
static void |
closeQuietly(java.io.Closeable... closeables)
Closes a
Closeable unconditionally. |
static void |
closeQuietly(java.io.Closeable closeable)
Closes a
Closeable unconditionally. |
static void |
closeQuietly(java.sql.Connection conn)
Closes a
Connection unconditionally. |
static void |
closeQuietly(java.io.InputStream input)
Closes an
InputStream unconditionally. |
static void |
closeQuietly(java.io.OutputStream output)
Closes an
OutputStream unconditionally. |
static void |
closeQuietly(java.io.Reader input)
Closes an
Reader unconditionally. |
static void |
closeQuietly(java.sql.ResultSet resultSet)
Closes a
AutoCloseable unconditionally. |
static void |
closeQuietly(java.nio.channels.Selector selector)
Closes a
Selector unconditionally. |
static void |
closeQuietly(java.net.ServerSocket sock)
Closes a
ServerSocket unconditionally. |
static void |
closeQuietly(java.net.Socket sock)
Closes a
Socket unconditionally. |
static void |
closeQuietly(java.sql.Statement... statements)
Closes a
AutoCloseable unconditionally. |
static void |
closeQuietly(java.sql.Statement statement)
Closes a
AutoCloseable unconditionally. |
static void |
closeQuietly(java.io.Writer output)
Closes an
Writer unconditionally. |
public static void close(java.net.URLConnection conn)
conn - the connection to close.public static void closeQuietly(java.io.Reader input)
Reader unconditionally.
Equivalent to Reader.close(), except any exceptions will be ignored. This is typically used in finally
blocks.
Example code:
char[] data = new char[1024];
Reader in = null;
try {
in = new FileReader("foo.txt");
in.read(data);
in.close(); // close errors are handled
} catch (Exception e) {
// error handling
} finally {
IOUtils.closeQuietly(in);
}
input - the Reader to close, may be null or already closedpublic static void closeQuietly(java.io.Writer output)
Writer unconditionally.
Equivalent to Writer.close(), except any exceptions will be ignored. This is typically used in finally
blocks.
Example code:
Writer out = null;
try {
out = new StringWriter();
out.write("Hello World");
out.close(); // close errors are handled
} catch (Exception e) {
// error handling
} finally {
IOUtils.closeQuietly(out);
}
output - the Writer to close, may be null or already closedpublic static void closeQuietly(java.io.InputStream input)
InputStream unconditionally.
Equivalent to InputStream.close(), except any exceptions will be ignored. This is typically used in
finally blocks.
Example code:
byte[] data = new byte[1024];
InputStream in = null;
try {
in = new FileInputStream("foo.txt");
in.read(data);
in.close(); // close errors are handled
} catch (Exception e) {
// error handling
} finally {
IOUtils.closeQuietly(in);
}
input - the InputStream to close, may be null or already closedpublic static void closeQuietly(java.io.OutputStream output)
OutputStream unconditionally.
Equivalent to OutputStream.close(), except any exceptions will be ignored. This is typically used in
finally blocks.
Example code:
byte[] data = "Hello, World".getBytes();
OutputStream out = null;
try {
out = new FileOutputStream("foo.txt");
out.write(data);
out.close(); // close errors are handled
} catch (IOException e) {
// error handling
} finally {
IOUtils.closeQuietly(out);
}
output - the OutputStream to close, may be null or already closedpublic static void closeQuietly(java.io.Closeable closeable)
Closeable unconditionally.
Equivalent to Closeable.close(), except any exceptions will be ignored. This is typically used in finally
blocks.
Example code:
Closeable closeable = null;
try {
closeable = new FileReader("foo.txt");
// process closeable
closeable.close();
} catch (Exception e) {
// error handling
} finally {
IOUtils.closeQuietly(closeable);
}
Closing all streams:
try {
return IOUtils.copy(inputStream, outputStream);
} finally {
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
}
closeable - the objects to close, may be null or already closedpublic static void closeQuietly(java.io.Closeable... closeables)
Closeable unconditionally.
Equivalent to Closeable.close(), except any exceptions will be ignored.
This is typically used in finally blocks to ensure that the closeable is closed even if an Exception was thrown
before the normal close statement was reached.
It should not be used to replace the close statement(s) which should be present for the non-exceptional
case.
It is only intended to simplify tidying up where normal processing has already failed and reporting close failure
as well is not necessary or useful.
Example code:
Closeable closeable = null;
try {
closeable = new FileReader("foo.txt");
// processing using the closeable; may throw an Exception
closeable.close(); // Normal close - exceptions not ignored
} catch (Exception e) {
// error handling
} finally {
IOUtils.closeQuietly(closeable); // In case normal close was skipped due to Exception
}
Closing all streams:
try {
return IOUtils.copy(inputStream, outputStream);
} finally {
IOUtils.closeQuietly(inputStream, outputStream);
}
closeables - the objects to close, may be null or already closedcloseQuietly(Closeable)public static void closeQuietly(java.net.Socket sock)
Socket unconditionally.
Equivalent to Socket.close(), except any exceptions will be ignored. This is typically used in finally
blocks.
Example code:
Socket socket = null;
try {
socket = new Socket("https://www.foo.com/", 443);
// process socket
socket.close();
} catch (Exception e) {
// error handling
} finally {
IOUtils.closeQuietly(socket);
}
sock - the Socket to close, may be null or already closedpublic static void closeQuietly(java.nio.channels.Selector selector)
Selector unconditionally.
Equivalent to Selector.close(), except any exceptions will be ignored. This is typically used in finally
blocks.
Example code:
Selector selector = null;
try {
selector = Selector.open();
// process socket
} catch (Exception e) {
// error handling
} finally {
IOUtils.closeQuietly(selector);
}
selector - the Selector to close, may be null or already closedpublic static void closeQuietly(java.net.ServerSocket sock)
ServerSocket unconditionally.
Equivalent to ServerSocket.close(), except any exceptions will be ignored. This is typically used in
finally blocks.
Example code:
ServerSocket socket = null;
try {
socket = new ServerSocket();
// process socket
socket.close();
} catch (Exception e) {
// error handling
} finally {
IOUtils.closeQuietly(socket);
}
sock - the ServerSocket to close, may be null or already closedpublic static void closeQuietly(java.sql.Connection conn)
Connection unconditionally.
Equivalent to Connection.close(), except any exceptions will be ignored. This is typically used in
finally blocks.
Example code:
Connection conn = null;
try {
conn = new Connection();
// process close
conn.close();
} catch (Exception e) {
// error handling
} finally {
IOUtils.closeQuietly(conn);
}
conn - the Connection to close, may be null or already closedpublic static void closeQuietly(java.sql.ResultSet resultSet)
AutoCloseable unconditionally.
Equivalent to ResultSet.close(), except any exceptions will be ignored. This is typically used in finally
blocks.
Example code:
AutoCloseable statement = null;
try {
statement = new Connection();
// process close
statement.close();
} catch (Exception e) {
// error handling
} finally {
IOUtils.closeQuietly(conn);
}
resultSet - the Connection to close, may be null or already closedpublic static void closeQuietly(java.sql.Statement statement)
AutoCloseable unconditionally.
Equivalent to Statement.close(), except any exceptions will be ignored. This is typically used in finally
blocks.
Example code:
AutoCloseable statement = null;
try {
statement = new Connection();
// process close
statement.close();
} catch (Exception e) {
// error handling
} finally {
IOUtils.closeQuietly(conn);
}
statement - the Connection to close, may be null or already closedpublic static void closeQuietly(java.sql.Statement... statements)
AutoCloseable unconditionally.
Equivalent to AutoCloseable.close(), except any exceptions will be ignored.
This is typically used in finally blocks to ensure that the closeable is closed even if an Exception was thrown
before the normal close statement was reached.
It should not be used to replace the close statement(s) which should be present for the non-exceptional
case.
It is only intended to simplify tidying up where normal processing has already failed and reporting close failure
as well is not necessary or useful.
Example code:
AutoCloseable closeable = null;
try {
closeable = new AutoCloseable();
// processing using the closeable; may throw an Exception
closeable.close(); // Normal close - exceptions not ignored
} catch (Exception e) {
// error handling
} finally {
IOUtils.closeQuietly(closeable); // In case normal close was skipped due to Exception
}
Closing all streams:
statements - the objects to close, may be null or already closedcloseQuietly(Statement)