Created
December 16, 2011 18:12
-
-
Save fge/1487203 to your computer and use it in GitHub Desktop.
Example code
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 net.sf.jpam; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.io.RandomAccessFile; | |
import java.io.Reader; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.FileChannel; | |
import java.util.regex.Pattern; | |
public final class Test | |
{ | |
private static final Pattern TAB = Pattern.compile("\t"); | |
private static class FileHolder | |
{ | |
private static final byte TABCHAR[] = "\t".getBytes(); | |
// Size of the buffer size | |
private static final int BUFSZ = 32768; | |
// Format string for a file | |
private static final String FORMAT = "/home/fge/t2.txt.%d"; | |
// The ByteBuffer | |
private final ByteBuffer buf = ByteBuffer.allocate(BUFSZ); | |
// The File object | |
private final File fd; | |
// RandomAccessFile | |
private final RandomAccessFile file; | |
FileHolder(final int index) | |
throws FileNotFoundException | |
{ | |
final String name = String.format(FORMAT, index); | |
fd = new File(name); | |
file = new RandomAccessFile(fd, "rw"); | |
} | |
public void write(final String s) | |
throws IOException | |
{ | |
final byte[] b = s.getBytes(); | |
if (buf.remaining() < b.length + TABCHAR.length) | |
flush(); | |
buf.put(b).put(TABCHAR); | |
} | |
private void flush() | |
throws IOException | |
{ | |
file.write(buf.array(), 0, buf.position()); | |
buf.position(0); | |
} | |
public void copyTo(final RandomAccessFile dst) | |
throws IOException | |
{ | |
flush(); | |
final FileChannel source = file.getChannel(); | |
final FileChannel destination = dst.getChannel(); | |
source.force(false); | |
final long len = source.size() - TABCHAR.length; | |
source.transferTo(0, len, destination); | |
dst.writeBytes("\n"); | |
} | |
public void tearDown() | |
throws IOException | |
{ | |
file.close(); | |
if (!fd.delete()) | |
System.err.println("Failed to remove file " + fd); | |
} | |
@Override | |
public String toString() | |
{ | |
return fd.toString(); | |
} | |
} | |
public static void main(final String... args) | |
throws IOException | |
{ | |
long before, after; | |
before = System.currentTimeMillis(); | |
final Reader r = new FileReader("/home/fge/t.txt"); | |
final BufferedReader reader = new BufferedReader(r); | |
/* | |
* Read first line, count the number of elements. All elements are | |
* separated by a single tab. | |
*/ | |
String line = reader.readLine(); | |
String[] elements = TAB.split(line); | |
final int nrLines = elements.length; | |
final FileHolder[] files = new FileHolder[nrLines]; | |
/* | |
* Initialize file descriptors | |
*/ | |
for (int i = 0; i < nrLines; i++) | |
files[i] = new FileHolder(i); | |
/* | |
* Write first lines, then all others | |
*/ | |
writeOneLine(elements, files); | |
while ((line = reader.readLine()) != null) { | |
elements = TAB.split(line); | |
writeOneLine(elements, files); | |
} | |
reader.close(); | |
r.close(); | |
after = System.currentTimeMillis(); | |
System.out.println("Read time: " + (after - before)); | |
before = System.currentTimeMillis(); | |
final RandomAccessFile out = new RandomAccessFile("/home/fge/t2.txt", | |
"rw"); | |
for (final FileHolder file: files) { | |
file.copyTo(out); | |
file.tearDown(); | |
} | |
out.getChannel().force(false); | |
out.close(); | |
after = System.currentTimeMillis(); | |
System.out.println("Write time: " + (after - before)); | |
System.exit(0); | |
} | |
private static void writeOneLine(final String[] elements, | |
final FileHolder[] fdArray) | |
throws IOException | |
{ | |
final int len = elements.length; | |
String element; | |
FileHolder file; | |
for (int index = 0; index < len; index++) { | |
element = elements[index]; | |
file = fdArray[index]; | |
file.write(element); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment