Skip to content

Instantly share code, notes, and snippets.

@mckornfield
Last active December 1, 2017 19:45
Show Gist options
  • Select an option

  • Save mckornfield/416d37d9b7b7e0da0ed5a921ae948eca to your computer and use it in GitHub Desktop.

Select an option

Save mckornfield/416d37d9b7b7e0da0ed5a921ae948eca to your computer and use it in GitHub Desktop.
Read Large File
import java.io.*
/*
Given a One GB file that prints statuses, determine a faster way to read the file and determine the current status.
To simplify, the file will only contain nothing, or a Status with a color, as well as a time (here just given as
an integer from 0 to the Length of the File - 1)
Another scenario could be that we have two files, one at one snapshot in time and one at a later snapshot in time,
with additional statuses added since the reading of the first file.
A third scenario has a status omitted, so a default status must be put if that status does not show up in the logs.
*/
public class ReadFile {
public static void main(String[] args) {
File file = generate1GbFile();
Status status = new Status();
long startTime = System.currentTimeMillis();
readFileAndUpdateStatus(file, status);
long endTime = System.currentTimeMillis();
System.out.println("Time elapsed: " + (endTime - startTime));
System.out.println(status.toString());
file.delete();
}
private void readFileAndUpdateStatus(File file, Status status) {
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
//TODO make reading better? faster? stronger?
if(line.contains("Status 1")) {
status.setStatusOne(line.split(":")[1].trim());
} else if(line.contains("Status 2")) {
status.setStatusTwo(line.split(":")[1].trim());
} else if(line.contains("Status 3")) {
status.setStatusThree(line.split(":")[1].trim());
}
}
fileReader.close();
}
private File generate1GbFile() {
RandomAccessFile f = new RandomAccessFile("t", "rw");
for(int i = 0; i < (1024*1024*1024); i++) {
//TODO make status random
f.writeChars(i + " Status 1: GREEN");
f.writeChars(i + " Status 2: RED");
f.writeChars(i + " Status 3: YELLOW");
}
return f;
}
private class Status {
String status1;
String status2;
String status3;
private void setStatusOne(String status) {
this.status1 = status;
}
private void setStatusTwo(String status) {
this.status2 = status;
}
private void setStatusThree(String status) {
this.status3 = status;
}
public String toString() {
return "Status 1 = " + status1 " | Status 2 = " + status2 + " Status 3 = " status3;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment