Skip to content

Instantly share code, notes, and snippets.

@zhugw
Last active April 13, 2017 13:50
Show Gist options
  • Save zhugw/f38981bcb37f764b0b7087f8b238404f to your computer and use it in GitHub Desktop.
Save zhugw/f38981bcb37f764b0b7087f8b238404f to your computer and use it in GitHub Desktop.
Java8重写拼接文件除去文件头尾的各行
public static String loadPemKey(String fileName) throws Exception {
// TODO: 可以考虑用Java8重写
BufferedReader br = new BufferedReader(new FileReader(PATH + fileName));
String s = br.readLine();
String str = "";
s = br.readLine();
while (s.charAt(0) != '-') {
//str += s + "\r";
str+=s;
s = br.readLine();
}
return str;
}
List<String> lines = Files.readAllLines(Paths.get("src/main/resources/privateKey/foo.pem"));
String reduce = lines.stream().filter(line->!line.startsWith("-")).reduce("", (line1, line2) -> line1 + line2);
String pemKey = TnGetSignUtil.loadPemKey("foo.pem");
assertEquals(pemKey,reduce);
      // 更简洁
String reduce = Files.lines(Paths.get(path)).filter(line->!line.startsWith("-")).collect(joining());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment