Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stuart-marks/4b6e2a14395fc1d7d745e3fc2d1d64d5 to your computer and use it in GitHub Desktop.
Save stuart-marks/4b6e2a14395fc1d7d745e3fc2d1d64d5 to your computer and use it in GitHub Desktop.
Processing Large Files in Java, Variation 2
--- ReadFileJavaApplicationBufferedReader1.java
+++ ReadFileJavaApplicationBufferedReader2.java
@@ -29,17 +29,12 @@
// get total line count
Instant lineCountStart = Instant.now();
- int lines = 0;
Instant namesStart = Instant.now();
ArrayList<String> names = new ArrayList<>();
// get the 432nd and 43243 names
- ArrayList<Integer> indexes = new ArrayList<>();
-
- indexes.add(1);
- indexes.add(433);
- indexes.add(43244);
+ int[] indexes = { 0, 432, 43243 };
// count the number of donations by month
Instant donationsStart = Instant.now();
@@ -53,16 +48,12 @@
System.out.println("Reading file using " + Caller.getName());
while ((readLine = b.readLine()) != null) {
- lines++;
// System.out.println(readLine);
// get all the names
String array1[] = readLine.split("\\|", 9);
String name = array1[7].strip();
names.add(name);
- if(indexes.contains(lines)){
- System.out.println("Name: " + names.get(lines - 1) + " at index: " + (lines - 1));
- }
if(name.contains(", ")) {
@@ -88,11 +79,15 @@
}
+ for (int i : indexes) {
+ System.out.println("Name: " + names.get(i) + " at index: " + (i));
+ }
+
Instant namesEnd = Instant.now();
long timeElapsedNames = Duration.between(namesStart, namesEnd).toMillis();
System.out.println("Name time: " + timeElapsedNames + "ms");
- System.out.println("Total file line count: " + lines);
+ System.out.println("Total file line count: " + names.size());
Instant lineCountEnd = Instant.now();
long timeElapsedLineCount = Duration.between(lineCountStart, lineCountEnd).toMillis();
System.out.println("Line count time: " + timeElapsedLineCount + "ms");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment