Created
January 9, 2018 14:01
-
-
Save onozaty/de8511125c4a1a872e08ccdf49196bea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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