我已经成功拆分了一个csv,但是有时它的某些文件块的末行不完整。该文件使用RandomAccessFile对象包装。这是我的代码:
long sourceSize = raf.length();
long bytesPerSplit = sourceSize / splits;
long remainingBytes = sourceSize % splits;
int maxReadBufferSize = 8 * 1024; // 8 KB
for (int destIx = 1; destIx <= splits; destIx++) {
// TODO: change so that first name is the year involved
try(BufferedOutputStream bw = new BufferedOutputStream(
new FileOutputStream(destination + "\\chunk" + destIx + ".csv"))) {
if (bytesPerSplit > maxReadBufferSize) {
long numReads = bytesPerSplit / maxReadBufferSize;
long numRemainingRead = bytesPerSplit % maxReadBufferSize;
for (int i = 0; i < numReads; i++) {
readWrite(raf, bw, maxReadBufferSize);
}
if (numRemainingRead > 0) readWrite(raf, bw, numRemainingRead);
} else readWrite(raf, bw, bytesPerSplit);
}
我想过要检查最后读取的字节数组是否事先在末尾有换行符,以及是否不增加要读取的缓冲区大小,但我不知道是否可以使用RandomAccessFile对象做到这一点