有以下这样的一个类public class TestData { private boolean transactionFlag;
private boolean success;
private long timpStamp;
private long bytes;
private int elapsed;
private String responseCode;
private String label;
private String threadName;
}想把这个类的内容输出到binary文件, 大致一共有500万个对象。我测试了下,10万个的读,就用了6秒。 有点慢了。 我想知道怎么读写binary文件更快呢?
貌似C的函数很快的。
Date d = new Date();
String inputputFileName = "c:\\testb";
SampleData sampleData = null; RandomAccessFile ra = null;
try {
ra = new RandomAccessFile(inputputFileName,"r"); sampleData = new SampleData();
for (int i = 0; i < size; i++) {
sampleData.readFromBinary(ra);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
} try {
if (ra != null) {
ra.close();
}
} catch (IOException e) {
e.printStackTrace();
}
Date l = new Date();
System.out.println("End! " + (l.getTime() - d.getTime())); public void readFromBinary(RandomAccessFile file) throws IOException {
transactionFlag = file.readBoolean();
success = file.readBoolean();
timpStamp = file.readLong();
bytes = file.readLong();
elapsed = file.readInt(); responseCode = file.readUTF();
label = file.readUTF();
threadName = file.readUTF();
}
private boolean success;
private long timpStamp;
private long bytes;
private int elapsed;
private String responseCode;
private String label;
private String threadName;
}想把这个类的内容输出到binary文件, 大致一共有500万个对象。我测试了下,10万个的读,就用了6秒。 有点慢了。 我想知道怎么读写binary文件更快呢?
貌似C的函数很快的。
Date d = new Date();
String inputputFileName = "c:\\testb";
SampleData sampleData = null; RandomAccessFile ra = null;
try {
ra = new RandomAccessFile(inputputFileName,"r"); sampleData = new SampleData();
for (int i = 0; i < size; i++) {
sampleData.readFromBinary(ra);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
} try {
if (ra != null) {
ra.close();
}
} catch (IOException e) {
e.printStackTrace();
}
Date l = new Date();
System.out.println("End! " + (l.getTime() - d.getTime())); public void readFromBinary(RandomAccessFile file) throws IOException {
transactionFlag = file.readBoolean();
success = file.readBoolean();
timpStamp = file.readLong();
bytes = file.readLong();
elapsed = file.readInt(); responseCode = file.readUTF();
label = file.readUTF();
threadName = file.readUTF();
}
java本来就没有c的速度快
更别说处理io了
还500万个对象,呵呵
我看RandomAccessFile的javadoc里有 FileChannel
MappedByteBuffer map(FileChannel.MapMode mode, long position, long size)
什么的。是不是用了这个会快呢?MappedByteBuffer里没有getBoolean 和putBoolean函数。 很奇怪啊。
能说具体点吗?
听说1.4以后RandomAccessFile被重构了,已经使用了nio包的内容了。
如果你的硬件状况不错的话
可以考虑提高jvm虚拟内存,扩到1024m,或者更大
int bufSize = 1024;
byte[] bs = new byte[bufSize];
ByteBuffer byteBuf = ByteBuffer.allocate(bufSize);
try {
FileChannel channel = new RandomAccessFile("c:\\testb","r").getChannel();
int n;
while(channel.read(byteBuf) != -1) {
n = byteBuf.position();
byteBuf.rewind();
byteBuf.get(bs); //System.out.print(new String(bs, 0, n));
byteBuf.clear();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
}这样来读可以快很多。 基本10万个,0.1秒就好了。 但是我怎么把byteBuf正确的转成类的对象呢?
要性能,改用c吧!要编码方便取java!鱼和熊掌不能兼得的!
buffer,用java.nio.ByteBuffer类,可以用其方法读int,double之类的数据,但没提供读字符串的方法.
10w个3.8秒
但是500w个 OutOfMemoryError: Java heap space
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file))); 这样做读取500万个对象只要6秒左右。http://ajava.org/code/io/360.html
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;public class TestWrite { public static void main(String[] args)throws Exception {
long sTime = System.currentTimeMillis();
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("d:\\zip\\bigData.txt")));
TestData td = new TestData();
long count = 500 * 10000 ;
for( ; count > 0; count--) {
oos.writeObject(td);
} oos.close();
System.out.print(System.currentTimeMillis() - sTime);
}}class TestData implements Serializable { private boolean transactionFlag;
private boolean success;
private long timpStamp;
private long bytes;
private int elapsed;
private String responseCode;
private String label;
private String threadName;
}控制台输出: 1622注:
1. 这里都是写入同一个对象,而不是new了500W个对象来写入.
2. 生成的文件大小为23.8 MB