0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

自分用Javaテキストファイル入出力

Posted at

テキストファイルの読み込み

小さなテキストファイルを一度に最後まで読み込む場合はjava.nio.file.FilesreadAllLinesで読み込める。

Puzzle.java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class Puzzle {
	public static void main(String[] args) {
		try {
			List<String> line = Files.readAllLines(Paths.get(args[0]), StandardCharsets.UTF_8);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

ファイルサイズ不明のテキストファイルを一行ずつ読み込む場合はjava.nio.file.Fileslinesで読み込める。
いずれも文字コードを指定できる。
いずれも例外IOExceptionを投げるので補足できるようにしておく。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?