Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Random;
public class TestWCJava2 {
public static void main(String args[]) throws IOException {
// generateFile();
try {
new TestWCJava2().run(args);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void generateFile() throws IOException {
Random r = new Random();
byte[] buffer = new byte[1024];
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("/tmp/three"))) {
for (int i = 0; i < 15 * 1024; i++) {
r.nextBytes(buffer);
bos.write(buffer);
}
}
}
void run(String files[]) throws IOException {
long t0 = System.nanoTime();
wordCount("/tmp/three");
long t1 = System.nanoTime();
System.out.println("Elapsed time: " + (t1 - t0) / 1000 + "us");
}
int wordCount(String fileName) throws IOException {
File f = new File(fileName);
FileChannel fc = new FileInputStream(f).getChannel();
MappedByteBuffer mem = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
int nlines = 0;
byte newline = '\n';
byte[] buffer = new byte[4096];
while(mem.hasRemaining()) {
int read = Math.min(mem.remaining(), buffer.length);
mem.get(buffer, 0, read);
for (int i = 0; i < read; i++) {
if (buffer[i] == newline) {
nlines++;
}
}
}
System.out.println(fileName + " lines: " + nlines);
return nlines;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment