存取檔案是基本的programming 技巧. 正好Java 8 出了nio package, 就試了用它將檔案當成string去存取.
private String readLineByLine(String filePath)
{
StringBuilder contentBuilder = new StringBuilder();
try (Stream<String> stream = Files.lines(Paths.get(filePath), StandardCharsets.ISO_8859_1))
{
stream.forEach(s -> contentBuilder.append(s).append("\n"));
}
catch (IOException e)
{
e.printStackTrace();
}
return contentBuilder.toString();
}
Leave a Reply