Skip to content

Instantly share code, notes, and snippets.

@shaobin0604
Created September 17, 2010 03:24
Show Gist options
  • Save shaobin0604/583611 to your computer and use it in GitHub Desktop.
Save shaobin0604/583611 to your computer and use it in GitHub Desktop.
WordMemo index generator
package com.example;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class IndexGen {
private static final byte CR = 0x0D;
private static final byte LF = 0x0A;
public static void main(String[] args) {
RandomAccessFile in = null;
RandomAccessFile out = null;
try {
in = new RandomAccessFile("tofel-word.csv", "r");
out = new RandomAccessFile("tofel-word.csv.idx", "rw");
long lastPos = 0;
long position = 0;
long length = in.length();
while ((position = in.getFilePointer()) < length) {
byte byte1 = (byte) in.readByte();
if (CR == byte1) {
if (in.getFilePointer() < length) {
byte byte2 = (byte) in.readByte();
if (LF == byte2) {
out.writeLong(lastPos);
out.writeLong(position - lastPos);
lastPos = position + 2;
}
}
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (in != null)
try {
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (out != null)
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment