Created
May 22, 2015 15:47
-
-
Save premrajm/c5f012ced316b7de17b1 to your computer and use it in GitHub Desktop.
Loading 1 GB file using BufferedSource
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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