直接用一点java调shell代码来说明吧。
第一种方式我称为竖直式,这种方式的特点就是挨个挨个处理catch,成功了再往下一步,如果遇见了则把这个catch点之前所有的资源释放。
优点是不会有逻辑错误
缺点是繁杂
public static CommandResult 竖直式(String command) {
Process process;
try {
process = Runtime.getRuntime().exec(command);
} catch (IOException e) {
return new CommandResult(CommandResult.RESULT_ERROR, null, e.getMessage());
}
try {
process.waitFor();
} catch (InterruptedException e) {//释放这个catch之前所有的资源
process.destroy();
return new CommandResult(CommandResult.RESULT_ERROR, null, e.getMessage());
}
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String tmep;
StringBuilder builder = new StringBuilder(256);
try {
while ((tmep = reader.readLine()) != null) {
builder.append(tmep);
builder.append("\n");
}
} catch (IOException e) {//释放这个catch之前所有的资源
try {
reader.close();
} catch (IOException e1) {
return new CommandResult(CommandResult.RESULT_ERROR, null, e1.getMessage());
}
process.destroy();
return new CommandResult(CommandResult.RESULT_ERROR, null, e.getMessage());
}
try {
reader.close();
} catch (IOException e) {//释放这个catch之前所有的资源
process.destroy();
return new CommandResult(CommandResult.RESULT_ERROR, null, e.getMessage());
}
process.destroy();
return new CommandResult(CommandResult.RESULT_SUCCESS, builder.toString(), null);
}
第二种我cheng称为统一式,特点是都放在try里面,然后根据资源是否为空释放资源。
优点是简洁明了
缺点是你时不时怀疑自己漏掉了逻辑,然后反复看这段代码
public static CommandResult 统一式(String command) {
Process process = null;
BufferedReader reader = null;
try {
process = Runtime.getRuntime().exec(command);
process.waitFor();
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String tmep;
StringBuilder builder = new StringBuilder(256);
while ((tmep = reader.readLine()) != null) {
builder.append(tmep);
builder.append("\n");
}
reader.close();
process.destroy();
return new CommandResult(CommandResult.RESULT_SUCCESS, builder.toString(), null);
} catch (IOException e) {
return new CommandResult(CommandResult.RESULT_ERROR, null, e.getMessage());
} catch (InterruptedException e) {
return new CommandResult(CommandResult.RESULT_ERROR, null, e.getMessage());
} finally {//全放在一个try里面,然后根据是否是null释放资源
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (process != null) {
process.destroy();
}
}
}
第三种我称为嵌套式,特点是try catch嵌套(这个方式算是java编程之道一书里推荐的吧),然后根据按层释放资源(这是我自己琢磨的)。
优点是不会漏掉任何一个资源
缺点是仅次于第一种的繁杂,也需要你仔细查看方法抛出的异常
public static CommandResult 嵌套式(String command) {
try {
Process process = Runtime.getRuntime().exec(command);
try {
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String tmep;
StringBuilder builder = new StringBuilder(256);
try {
while ((tmep = reader.readLine()) != null) {
builder.append(tmep);
builder.append("\n");
}
return new CommandResult(CommandResult.RESULT_SUCCESS, builder.toString(), null);
} catch (IOException e) {
return new CommandResult(CommandResult.RESULT_ERROR, null, e.getMessage());
} finally {
try {
reader.close();
} catch (IOException e) {
return new CommandResult(CommandResult.RESULT_ERROR, null, e.getMessage());
}
}
} catch (InterruptedException e) {
return new CommandResult(CommandResult.RESULT_ERROR, null, e.getMessage());
} finally {
process.destroy();
}
} catch (IOException e) {
return new CommandResult(CommandResult.RESULT_ERROR, null, e.getMessage());
}
}
第四种则是java 7的新特性,try()自动释放资源,和嵌套式一起使用如下。
public static CommandResult exec(String command) {
try {
Process process = Runtime.getRuntime().exec(command);
try {
process.waitFor();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String tmep;
StringBuilder builder = new StringBuilder(256);
while ((tmep = reader.readLine()) != null) {
builder.append(tmep);
builder.append("\n");
}
return new CommandResult(CommandResult.RESULT_SUCCESS, builder.toString(), null);
} catch (IOException e) {
return new CommandResult(CommandResult.RESULT_ERROR, null, e.getMessage());
}
} catch (InterruptedException e) {
return new CommandResult(CommandResult.RESULT_ERROR, null, e.getMessage());
} finally {
process.destroy();
}
} catch (IOException e) {
return new CommandResult(CommandResult.RESULT_ERROR, null, e.getMessage());
}
}