Java 关闭数据流工具类
工具类
异处使用:
public static boolean writeFileFromIS(File file, final InputStream is, boolean append) { if (!FileUtils.createOrExistsFile(file) || is == null) return false; OutputStream os = null; try { os = new BufferedOutputStream(new FileOutputStream(file, append)); byte data[] = new byte[1024]; int len; while ((len = is.read(data, 0, 1024)) != -1) { os.write(data, 0, len); } return true; } catch (IOException e) { e.printStackTrace(); return false; } finally { CloseUtils.closeIO(is, os); } }
public final class CloseUtils { private CloseUtils() { throw new UnsupportedOperationException("u can't instantiate me..."); } /** * 关闭IO * * @param closeables closeables */ public static void closeIO(Closeable... closeables) { if (closeables == null) return; for (Closeable closeable : closeables) { if (closeable != null) { try { closeable.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 安静关闭IO * * @param closeables closeables */ public static void closeIOQuietly(Closeable... closeables) { if (closeables == null) return; for (Closeable closeable : closeables) { if (closeable != null) { try { closeable.close(); } catch (IOException ignored) { } } } }} public static void closeQuietly(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (IOException ioe) { // ignore } } }
版权声明:如无特殊标注,文章均为本站原创,转载时请以链接形式注明文章出处。
评论