Skip to content

Instantly share code, notes, and snippets.

@mo-ba
Last active November 2, 2022 20:38
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mo-ba/a4bcb10ca709c9f31f014cbf655768d7 to your computer and use it in GitHub Desktop.
Save mo-ba/a4bcb10ca709c9f31f014cbf655768d7 to your computer and use it in GitHub Desktop.
read lines from file
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
String path = "resources/testfile.txt";
measureTime("BufferedReader.readLine() into LinkedList", Main::bufferReaderToLinkedList, path);
measureTime("BufferedReader.readLine() into ArrayList", Main::bufferReaderToArrayList, path);
measureTime("Files.readAllLines()", Main::readAllLines, path);
measureTime("Scanner.nextLine() into ArrayList", Main::scannerArrayList, path);
measureTime("Scanner.nextLine() into LinkedList", Main::scannerLinkedList, path);
measureTime("RandomAccessFile.readLine() into ArrayList", Main::randomAccessFileArrayList, path);
measureTime("RandomAccessFile.readLine() into LinkedList", Main::randomAccessFileLinkedList, path);
System.out.println("-----------------------------------------------------------");
}
private static void measureTime(String name, Function<String, List<String>> fn, String path) {
System.out.println("-----------------------------------------------------------");
System.out.println("run: " + name);
long startTime = System.nanoTime();
List<String> l = fn.apply(path);
long estimatedTime = System.nanoTime() - startTime;
System.out.println("lines: " + l.size());
System.out.println("estimatedTime: " + estimatedTime / 1_000_000_000.);
}
private static List<String> bufferReaderToLinkedList(String path) {
return bufferReaderToList(path, new LinkedList<>());
}
private static List<String> bufferReaderToArrayList(String path) {
return bufferReaderToList(path, new ArrayList<>());
}
private static List<String> bufferReaderToList(String path, List<String> list) {
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(
path));
String line = reader.readLine();
while (line != null) {
line = reader.readLine();
list.add(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
private static List<String> readAllLines(String path) {
try {
return Files.readAllLines(Paths.get(path));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static List<String> randomAccessFileLinkedList(String path) {
return randomAccessFile(path, new LinkedList<>());
}
private static List<String> randomAccessFileArrayList(String path) {
return randomAccessFile(path, new ArrayList<>());
}
private static List<String> randomAccessFile(String path, List<String> list) {
try {
RandomAccessFile file = new RandomAccessFile(path, "r");
String str;
while ((str = file.readLine()) != null) {
list.add(str);
}
file.close();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
private static List<String> scannerLinkedList(String path) {
return scanner(path, new LinkedList<>());
}
private static List<String> scannerArrayList(String path) {
return scanner(path, new ArrayList<>());
}
private static List<String> scanner(String path, List<String> list) {
try {
Scanner scanner = new Scanner(new File(path));
while (scanner.hasNextLine()) {
list.add(scanner.nextLine());
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return list;
}
}
-----------------------------------------------------------
run: BufferedReader.readLine() into LinkedList
lines: 1000000
estimatedTime: 0.105118655
-----------------------------------------------------------
run: BufferedReader.readLine() into ArrayList
lines: 1000000
estimatedTime: 0.072696934
-----------------------------------------------------------
run: Files.readAllLines()
lines: 1000000
estimatedTime: 0.087753316
-----------------------------------------------------------
run: Scanner.nextLine() into ArrayList
lines: 1000000
estimatedTime: 0.743121734
-----------------------------------------------------------
run: Scanner.nextLine() into LinkedList
lines: 1000000
estimatedTime: 0.867049885
-----------------------------------------------------------
run: RandomAccessFile.readLine() into ArrayList
lines: 1000000
estimatedTime: 11.413323046
-----------------------------------------------------------
run: RandomAccessFile.readLine() into LinkedList
lines: 1000000
estimatedTime: 11.423862897
-----------------------------------------------------------
@jpvs0101
Copy link

jpvs0101 commented Nov 7, 2018

Most important info is missing:

Device config on which you tested?

@mo-ba
Copy link
Author

mo-ba commented May 24, 2019

I created the gist so you can test it on your device...
... but I tested on:
Ubuntu 16.04
Java8
cpu: Intel i7-6700K @ 4.00Ghz
ssd: Kingston HyperX Fury: SHFS37A
memory: Kingston HyperX Fury: KHX2666C15D4/8GB @ 2666GHz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment