public class IOUtils extends Object
copy from 'org.apache.commons.io.IOUtils'.
| 限定符和类型 | 方法和说明 |
|---|---|
static void |
close(URLConnection conn)
Closes a URLConnection.
|
static void |
closeQuietly(Closeable closeable)
Unconditionally close a
Closeable. |
static void |
closeQuietly(InputStream input)
Unconditionally close an
InputStream. |
static void |
closeQuietly(OutputStream output)
Unconditionally close an
OutputStream. |
static void |
closeQuietly(Reader input)
Unconditionally close an
Reader. |
static void |
closeQuietly(Selector selector)
Unconditionally close a
Selector. |
static void |
closeQuietly(ServerSocket sock)
Unconditionally close a
ServerSocket. |
static void |
closeQuietly(Socket sock)
Unconditionally close a
Socket. |
static void |
closeQuietly(Writer output)
Unconditionally close a
Writer. |
public static void close(URLConnection conn)
conn - the connection to close.public static void closeQuietly(Reader input)
Reader.
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(Writer output)
Writer.
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(InputStream input)
InputStream.
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(OutputStream output)
OutputStream.
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(Closeable closeable)
Closeable.
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);
}
closeable - the object to close, may be null or already closedpublic static void closeQuietly(Socket sock)
Socket.
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("http://www.foo.com/", 80);
// 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(Selector selector)
Selector.
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(ServerSocket sock)
ServerSocket.
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 closedCopyright © 2017. All rights reserved.