「Java入門」FileOutputStreamでファイルを書き込むサンプル

サンプルコード

//ファイルパス
Path path = Paths.get(“d:\\data.txt");
//文字列
List<String> lines = Stream.of(“data11", “data12", “data13").collect(Collectors.toList());

try (
// 追記true、新規false
FileOutputStream fos = new FileOutputStream(path.toFile(), true);
OutputStreamWriter osw = new OutputStreamWriter(fos, Charset.forName(“UTF-8"));
PrintWriter pw = new PrintWriter(osw)) {

for (String ct : lines) {
pw.println(ct);
}
} catch (Exception e) {
e.printStackTrace();
}

Software

Posted by arkgame