
書き込み
PrintWriter pw = null;
try {
pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(path), "UTF-8"));
pw.println("Hello World");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} finally {
if (pw != null) {
pw.close();
}
}
読み込み
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
書き込み Java7から、try-with-resources文が使えるようになった。それ以外は、Java6までと同様。
try (PrintWriter pw = new PrintWriter(
Files.newBufferedWriter(Paths.get(path), Charset.forName("UTF-8"), StandardOpenOption.CREATE))) {
pw.println("Hello World");
} catch (IOException e) {
e.printStackTrace();
}
読み込み。 Java7から、try-with-resources文が使えるようになった。それ以外は、Java6までと同様。
try (BufferedReader br = Files.newBufferedReader(Paths.get(path), Charset.forName("UTF-8"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
読み込み。 Java8から、ストリームAPI、ラムダ式が使えるようになった。
try (Stream<String> stream = Files.lines(Paths.get(path), Charset.forName("UTF-8"))) {
stream.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
書き込み。 Java7から、Files,Pathsが使えるようになった。
List<String> lines = new ArrayList<String>();
lines.add("Hello World");
try {
Files.write(Paths.get(path), lines, Charset.forName("UTF-8"), StandardOpenOption.CREATE);
} catch (IOException e) {
e.printStackTrace();
}
読み込み。 Java7から、Files,Pathsが使えるようになった。
try {
List<String> lines = Files.readAllLines(Paths.get(path), Charset.forName("UTF-8"));
System.out.println(StringUtils.join(lines, System.lineSeparator()));
} catch (IOException e) {
e.printStackTrace();
}
書き込み
// リストからファイル書き込み
List<String> lines = new ArrayList<String>();
lines.add("Hello Apache Commons IO");
try {
FileUtils.writeLines(new File(path), "UTF-8", lines, SystemUtils.LINE_SEPARATOR);
} catch (IOException e) {
e.printStackTrace();
}
// 文字列からファイル書き込み
String text = "Hello Apache Commons IO\n" + "サンプルテキスト\n";
try {
FileUtils.writeStringToFile(new File(path), text, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
読み込み
// ファイルからリストへ読み込み
try {
List<String> lines = FileUtils.readLines(new File(path), "UTF-8");
System.out.println(StringUtils.join(lines, SystemUtils.LINE_SEPARATOR));
} catch (IOException e) {
e.printStackTrace();
}
// ファイルから文字列へ読み込み
try {
String text = FileUtils.readFileToString(new File(path), "UTF-8");
System.out.println(text);
} catch (IOException e) {
e.printStackTrace();
}
読み込み。 Java5では、Scannerが使えるようになった。空白や改行などでトークン分割された文字列を順次読み込む。
Scanner scanner = null;
try {
scanner = new Scanner(new File(path), "UTF-8");
while (scanner.hasNext()) {
String line = scanner.next();
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (scanner != null) {
scanner.close();
}
}
[PageInfo]
LastUpdate: 2016-05-27 21:15:48, ModifiedBy: kurukuru-papa
[Permissions]
view:all, edit:admins, delete/config:admins