62,628
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] arrs) throws IOException {
// Files.lines # 遍历每行
// 多去所有内容
Files.readAllLines(Paths.get("g://src.txt")).forEach(line -> {
List<String> nums = Arrays.asList(line.split(",")).stream().collect(Collectors.toList());
Double average = nums.stream().mapToDouble(Double::parseDouble).average().getAsDouble();
try {
StandardOpenOption option = Files.exists(Paths.get("g://dst.txt"))? StandardOpenOption.APPEND : StandardOpenOption.CREATE;
Files.write(Paths.get("g://dst.txt"), (average + "\r\n").getBytes(), option);
} catch (IOException e) {
e.getStackTrace();
//igore
}
});
}