Guest User

Untitled

a guest
Jan 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.nio.charset.StandardCharsets;
  6. import java.nio.file.Files;
  7. import java.nio.file.Paths;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. public class FileReadExample {
  12.  
  13. public List<String> readAllLines(String filePath) throws IOException {
  14.  
  15. // Java7以降(NIO2利用)
  16. return Files.readAllLines(Paths.get(filePath), StandardCharsets.UTF_8);
  17. }
  18.  
  19. public List<String> readAllLinesOld(String filePath) throws IOException {
  20.  
  21. // Java6以下の場合
  22. // (StandardCharsetsもJava7から)
  23. BufferedReader reader = new BufferedReader(
  24. new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
  25.  
  26. List<String> allLines = new ArrayList<>();
  27.  
  28. try {
  29. String line;
  30. while ((line = reader.readLine()) != null) {
  31. allLines.add(line);
  32. }
  33.  
  34. return allLines;
  35.  
  36. } finally {
  37. reader.close();
  38. }
  39. }
  40. }
Add Comment
Please, Sign In to add comment