Skip to content

Instantly share code, notes, and snippets.

@onozaty
Created January 9, 2018 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onozaty/de8511125c4a1a872e08ccdf49196bea to your computer and use it in GitHub Desktop.
Save onozaty/de8511125c4a1a872e08ccdf49196bea to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class FileReadExample {
public List<String> readAllLines(String filePath) throws IOException {
// Java7以降(NIO2利用)
return Files.readAllLines(Paths.get(filePath), StandardCharsets.UTF_8);
}
public List<String> readAllLinesOld(String filePath) throws IOException {
// Java6以下の場合
// (StandardCharsetsもJava7から)
BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
List<String> allLines = new ArrayList<>();
try {
String line;
while ((line = reader.readLine()) != null) {
allLines.add(line);
}
return allLines;
} finally {
reader.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment