该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
CloseResources类:-------------------------------------------------------------------------
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class CloseResources {
private static final Object[] PARAMS = new Object[0];
private static boolean invokeCloseMethod(Object o, Method[] methods) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
for (Method method : methods) {
//System.out.println(method.getName());
if (method.getName().equals("close") && method.getParameterTypes().length == 0) {
System.out.println("############close " + o.getClass());
method.invoke(o, PARAMS);
return true;
}
}
return false;
}
public static void close(Object o, ExceptionHandle handle) {
if (o == null) {//如果为空就不调用close()了,免得空指针异常
return;
}
Class> clazz = o.getClass();
try {
if (!invokeCloseMethod(o, clazz.getDeclaredMethods())) {//如果重写了close(),就调用重写的
invokeCloseMethod(o, clazz.getMethods());//如果没有重写close(),就调用继承的close()
}
} catch (SecurityException e) {
e.printStackTrace();
if (handle != null) {
handle.handleSecurityException(e);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
if (handle != null) {
handle.handleIllegalAccessException(e);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
if (handle != null) {
handle.handleIllegalArgumentException(e);
}
} catch (InvocationTargetException e) {
e.printStackTrace();
if (handle != null) {
handle.handleInvocationTargetException(e);
}
} catch (Exception e) {
e.printStackTrace();
if (handle != null) {
handle.handleException(e);
}
}
}
public static void close(Object o) {
close(o, null);
}
public static void closeCollection(Collection> collection) {
for (Object o : collection) {
close(o, null);
}
collection.clear();
}
public static void closeMap(Map map) {
Set keys = map.keySet();
for (K key : keys) {
close(map.get(key), null);
}
map.clear();
}
public static void main(String[] args) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
FileChannel channel = null;
List list = new ArrayList();
//Map map = new HashMap();
try {
FileInputStream fis = new FileInputStream("c:/1.txt");
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(new FileOutputStream("c:/2.txt"));
channel = fis.getChannel();
list.add(bis);
list.add(bos);
//map.put("bis", bis);
//map.put("bos", bos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
//CloseResources.close(bis);//关闭bio处理流,fis节点流也会被关闭,而channel也会关闭
//CloseResources.close(channel);//重复关闭也没事
//CloseResources.close(bos);
CloseResources.close(System.in, new ExceptionHandle() {//带异常处理的
@Override
public void handleException(Exception e) {
System.out.println("处理bis的 handleException()");
}
});
//CloseResources.closeCollection(list);//可以关闭collection中的资源,并清空collection
//CloseResources.closeMap(map);//可以关闭map中的资源,并清空map
}
}
}
ExceptionHandler类:-----------------------------------------------------------------------------
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
public class ExceptionHandle {
public void handleSecurityException(SecurityException e) {}
public void handleIllegalAccessException(IllegalAccessException e) {}
public void handleIllegalArgumentException(IllegalArgumentException e) {}
public void handleInvocationTargetException(InvocationTargetException e) {}
public void handleException(Exception e) {}
}