Skip to content

Instantly share code, notes, and snippets.

@premrajm
Created May 22, 2015 15:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save premrajm/c5f012ced316b7de17b1 to your computer and use it in GitHub Desktop.
Save premrajm/c5f012ced316b7de17b1 to your computer and use it in GitHub Desktop.
Loading 1 GB file using BufferedSource
package test;
import com.google.common.base.CharMatcher;
import com.google.common.base.Stopwatch;
import okio.BufferedSource;
import okio.Okio;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
BufferedSource source = Okio.buffer(Okio.source(Paths.get("<place 1 gb file here>")));
CharMatcher matcher = CharMatcher.is('\n');
long lineCount = 0;
Stopwatch watch = Stopwatch.createStarted();
while(!source.exhausted()) {
lineCount += matcher.countIn(source.readString(StandardCharsets.US_ASCII));
}
System.out.println(lineCount);
System.out.println("Finished in " + watch.stop());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment