java.nio vs java.io
早々コードね.ダンプ用のコードだけどちょっといじればランダム用もできる.
ランダムのほうはごちゃごちゃ混ざっているので,ちょっと勘弁して(汗
- java.io のほうのコード
import java.io.IOException; import java.io.RandomAccessFile; public class Bin2Txt { public static final int LONG_SIZE = 8; public static final int DATA_SIZE = 4 + 8; public static void main(String[] args) throws IOException { if(args.length != 2) { System.err.println("java Bin2Txt <index file> <data file>"); System.exit(-1); } String indexfile = args[0]; String datafile = args[1]; RandomAccessFile index = new RandomAccessFile(indexfile, "r"); RandomAccessFile data = new RandomAccessFile(datafile, "r"); long size = index.length() / LONG_SIZE - 1; long offset, length; for(int i = 0; i < size; ++i) { index.seek(i * LONG_SIZE); offset = index.readLong(); length = index.readLong(); data.seek(offset); while(offset < length) { System.out.println(i + " " + data.readInt() + " " + data.readDouble()); offset += DATA_SIZE; } } data.close(); index.close(); } }
- java.nio のほうのコード
import java.io.FileInputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.LongBuffer; import java.nio.channels.FileChannel; import java.nio.channels.FileChannel.MapMode; public class Bin2Txt2 { public static final int LONG_SIZE = 8; public static final int DATA_SIZE = 4 + 8; public static void main(String[] args) throws IOException { if(args.length != 2) { System.err.println("java Bin2Txt2 <index file> <data file>"); System.exit(-1); } String indexfile = args[0]; String datafile = args[1]; FileChannel indexchannel = new FileInputStream(indexfile).getChannel(); FileChannel datachannel = new FileInputStream(datafile).getChannel(); LongBuffer indexbuf = indexchannel.map(MapMode.READ_ONLY, 0, indexchannel.size()).asLongBuffer(); ByteBuffer databuf = datachannel.map(MapMode.READ_ONLY, 0, datachannel.size()); long size = indexchannel.size() / LONG_SIZE - 1; long offset, length; for(int i = 0; i < size; ++i) { indexbuf.position(i); offset = indexbuf.get(); length = indexbuf.get(); databuf.position((int)offset); while(offset < length) { System.out.println(i + " " + databuf.getInt() + " " + databuf.getDouble()); offset += DATA_SIZE; } } datachannel.close(); indexchannel.close(); } }
- これはデータを作成するほう.こっちは別にnioは使ってない.
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.RandomAccessFile; import java.util.regex.Pattern; public class Txt2Bin { private static final Pattern p = Pattern.compile("\\s+"); public static void main(String[] args) throws IOException { if(args.length != 3) { System.err.println("java Txt2Bin <in file> <out index file> <out data file>"); System.exit(-1); } File infile = new File(args[0]); File indexfile = new File(args[1]); File datafile = new File(args[2]); if(indexfile.exists()) { System.err.println("delete file: " +indexfile); indexfile.delete(); } if(datafile.exists()) { System.err.println("delete file: " +datafile); datafile.delete(); } int last = 0; BufferedReader br = new BufferedReader(new FileReader(infile)); RandomAccessFile index = new RandomAccessFile(indexfile, "rw"); RandomAccessFile data = new RandomAccessFile(datafile, "rw"); while(br.ready()) { String[] f = p.split(br.readLine()); int i = Integer.parseInt(f[0]), j = Integer.parseInt(f[1]); double d = Double.parseDouble(f[2]); long locate = data.getFilePointer(); while(last <= i) { index.writeLong(locate); ++last; } data.writeInt(j); data.writeDouble(d); } index.writeLong(data.getFilePointer()); data.close(); index.close(); br.close(); } }