Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mouamle.projects.meh;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.nio.file.Files;
- public class BigMeh {
- /**
- * This method is the fastest because it uses the buffer so it literally reads
- * the file (line by line)
- */
- public static void readLines(String fileName) throws Exception {
- BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));
- String line;
- while ((line = reader.readLine()) != null)
- System.out.println(line);
- reader.close();
- }
- /**
- * This method loads the file lines into a stream which is faster than loading
- * it to a List
- */
- public static void loadLinesStream(String fileName) throws Exception {
- Files.lines(new File(fileName).toPath()).forEach(System.out::println);
- }
- /**
- * This method loads the file lines into List of String so you could have the
- * full file size loaded into memory
- */
- public static void loadLinesList(String fileName) throws Exception {
- Files.readAllLines(new File(fileName).toPath()).forEach(System.out::println);
- }
- public static void main(String[] args) throws Exception {
- readLines("THE FILE NAME");
- loadLinesStream("THE FILE NAME");
- loadLinesList("THE FILE NAME");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement