如何使这段代码更简单?

时间:2017-06-21 06:42:47

标签: java

我想问一些关于如何使这段代码更简单的提示

    private void outputPatternList(String message) throws IOException {

    try (BufferedWriter out = new BufferedWriter(new FileWriter(patternList, true))) {
        out.write(message);
        out.write("\n");
    }
}

1 个答案:

答案 0 :(得分:1)

您需要将代码重构为:

private void outputPatternList(List<String> lines) throws IOException {
    StringBuilder builder  = new StringBuilder();
    for (String line : lines) {
         builder.append(line).append("/n");
    }
    String message = builder.toString().trim();
    try (BufferedWriter out = new BufferedWriter(new FileWriter(patternList, true))) {
        out.write(message);
    }
}