Java 将ByteArray写入文件问题
所以我有两种方法,当我使用断言测试来测试它们时,它失败了,我一辈子都不知道为什么,所以如果有人能帮助我,我将不胜感激 这些方法应该将一个块写入现有文件,然后读取所述块并确保断言有效 写入方法:Java 将ByteArray写入文件问题,java,file,bytearray,assertion,Java,File,Bytearray,Assertion,所以我有两种方法,当我使用断言测试来测试它们时,它失败了,我一辈子都不知道为什么,所以如果有人能帮助我,我将不胜感激 这些方法应该将一个块写入现有文件,然后读取所述块并确保断言有效 写入方法: public void writeBlock(int blockNum, AbstractDBFile f, AbstractBlock b) throws IOException { DBFile dbf = (DBFile) f; Block
public void writeBlock(int blockNum, AbstractDBFile f, AbstractBlock b)
throws IOException {
DBFile dbf = (DBFile) f;
Block blk = (Block) b;
if (blockNum >= dbf.totalNumOfBlocks){
dbf.totalNumOfBlocks++;
}
int header = 4096;
RandomAccessFile file = new RandomAccessFile(dbf.fileName, "rw");
file.seek(header + (blockNum) * 4096);
file.write(blk.getData(), 0, 4096);
file.close();
f.curBlockPos = blockNum + 1;
}
读取方法:
public AbstractBlock readBlock(int blockNum, AbstractDBFile f)
throws IOException {
f.setCurBlockPos(blockNum);
DBFile f2 = new DBFile();
f2.setCurBlockPos(f.getCurBlockPos());
f2.setFileName(f.getFileName());
Block block = new Block();
int currentByte = f2.getCurBlockPos() * 4096;
byte[] data = new byte[4096];
String filename = f2.getFileName();
File ourFile = new File(filename);
RandomAccessFile file = new RandomAccessFile(ourFile, "r");
FileChannel inChannel = file.getChannel();
ByteBuffer bb = ByteBuffer.allocate(currentByte);
inChannel.read(bb);
while(inChannel.read(bb)>0){
bb.flip();
for (int i =0; i<bb.limit(); i++){
data[i]=bb.get();
block.setData(data);
}
bb.clear();
}
inChannel.close();
file.close();
return block;
}
public AbstractBlock readBlock(int blockNum,abstractdbf文件)
抛出IOException{
f、 设置路缘锁定位置(blockNum);
DBFile f2=新的DBFile();
f2.设置路缘锁定位置(f.Get路缘锁定位置());
f2.setFileName(f.getFileName());
块=新块();
int currentByte=f2.getCurBlockPos()*4096;
字节[]数据=新字节[4096];
字符串文件名=f2.getFileName();
文件ourFile=新文件(文件名);
RandomAccessFile文件=新的RandomAccessFile(我们的文件,“r”);
FileChannel inChannel=file.getChannel();
ByteBuffer bb=ByteBuffer.allocate(当前字节);
inChannel.read(bb);
而(在通道读取(bb)>0){
bb.flip();
对于(int i=0;i关于数组的什么是不相等的?长度还是元素值?@和y Turner元素值是问题所在?我建议您在setData
调用之后(在writeBlock
之前)添加断言,以查看数据是否正确?我不确定是否会。您是否使用Java 7+?如果是:1.使用JSR 203,2.使用try with-resources@AndyTurnersetData是正确的
public class Block extends AbstractBlock {
@Override
public byte[] getData() {
// TODO Auto-generated method stub
/*byte[] data = new byte [4096];
data[0] = 10;
data[5] = 20;*/
return data;
}
@Override
public void setData(byte[] d) throws IOException {
int freeByte = -1;
for(int i = 0; i < data.length && freeByte < 0; i++)
if(data[i] == 0)
freeByte = i;
for(int i = freeByte, j = 0; j < d.length; i++, j++)
data[i] = d[j];
}
}
public void testWriteRead() throws IOException {
StorageManager manager = new StorageManager();
DBFile file = (DBFile)manager.createFile("File1");
byte [] write = new byte[4096];
write[0] = 10;
write[5] = 20;
Block block = new Block();
block.setData(write);
manager.writeBlock(0, file, block);
Block b = (Block) manager.readBlock(0, file);
assertTrue(areEqual(write,b.getData()));
}
private boolean areEqual(byte [] array1, byte [] array2) {
if(array1.length != array2.length)
return false;
for(int i = 0 ; i < array1.length ; i++)
if(array1[i] != array2[i])
return false;
return true;
}