Advertisement
Mouamle

Meh

Nov 15th, 2017
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. package mouamle.projects.meh;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.nio.file.Files;
  7.  
  8. public class BigMeh {
  9.  
  10.     /**
  11.      * This method is the fastest because it uses the buffer so it literally reads
  12.      * the file (line by line)
  13.      */
  14.     public static void readLines(String fileName) throws Exception {
  15.         BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));
  16.  
  17.         String line;
  18.         while ((line = reader.readLine()) != null)
  19.             System.out.println(line);
  20.  
  21.         reader.close();
  22.     }
  23.  
  24.     /**
  25.      * This method loads the file lines into a stream which is faster than loading
  26.      * it to a List
  27.      */
  28.     public static void loadLinesStream(String fileName) throws Exception {
  29.         Files.lines(new File(fileName).toPath()).forEach(System.out::println);
  30.     }
  31.  
  32.     /**
  33.      * This method loads the file lines into List of String so you could have the
  34.      * full file size loaded into memory
  35.      */
  36.     public static void loadLinesList(String fileName) throws Exception {
  37.         Files.readAllLines(new File(fileName).toPath()).forEach(System.out::println);
  38.     }
  39.  
  40.     public static void main(String[] args) throws Exception {
  41.         readLines("THE FILE NAME");
  42.         loadLinesStream("THE FILE NAME");
  43.         loadLinesList("THE FILE NAME");
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement