我创建了一个使用RandomAccessFile
预分配大小的文件。但是当我FileOutputStream
用来写入相同文件时,它正在改变文件的大小。有什么办法可以阻止这种使用FileOutputStream
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.io.Serializable;
public class testFileSize {
public static class Status implements Serializable {
}
public static void preAllocate(String path, long maxSize, boolean preAllocate)
throws IOException {
RandomAccessFile raf = new RandomAccessFile(path, "rw");
try {
raf.setLength(maxSize);
} finally {
raf.close();
}
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
FileOutputStream fileOutput = null;
ObjectOutputStream objectOutput = null;
try {
final File f = new File("/tmp/test.bin");
preBlow(f.getAbsolutePath(), 2048, false);
Status s = new Status();
fileOutput = new FileOutputStream(f);
objectOutput = new ObjectOutputStream(fileOutput);
objectOutput.writeObject(new Status());
objectOutput.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
objectOutput.close();
fileOutput.close();
}
}
}