Java 将4D阵列保存到文件时遇到问题
我有一些代码似乎没有按它应该的方式运行。关键是获取一个256x128x256x2整数数组,将其拆分为256个16x128x16x2块,将这些块处理为一个字节数组,然后将该字节数组添加到要保存的主字节数组中Java 将4D阵列保存到文件时遇到问题,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我有一些代码似乎没有按它应该的方式运行。关键是获取一个256x128x256x2整数数组,将其拆分为256个16x128x16x2块,将这些块处理为一个字节数组,然后将该字节数组添加到要保存的主字节数组中chunkdata[]在保存之前可以,但是在保存之后,除了前4096个字节之外,整个文件都是空白的。位置表(文件中每个块的位置)在那里,前四个字节的“块头”在那里,其他的都是0,这是不应该发生的 public void createFile(int[][][][] map){ byte[
chunkdata[]
在保存之前可以,但是在保存之后,除了前4096个字节之外,整个文件都是空白的。位置表(文件中每个块的位置)在那里,前四个字节的“块头”在那里,其他的都是0,这是不应该发生的
public void createFile(int[][][][] map){
byte[] file = new byte[fileLength]; //22,024,192 bytes long
System.arraycopy(Sector.locationTable, 0, file, 0, Sector.locationTable.length); //This works as it should
for(int cx = 0; cx < 16; cx++)
{
for(int cz = 0; cz < 16; cz++)
{
int start = sectorLength+cx*(sectorLength*chunkSectorLength)+cz*(chunkRows*sectorLength*chunkSectorLength); //this algorithm works, just rather hideous
int[][][][] chunk = getChunk(map, cx * 16, cz * 16); //This works as it should
byte[] chunkdata = putChunk(chunk); //The data from this is correct
int counter = 0;
for(int i=start;i<chunkdata.length;i++){
file[i]=chunkdata[counter]; //Data loss here?
counter++;
}
}
}
System.out.println("Saving file...");
writeFile(file, fileLocation);
}
public static void writeFile(byte[] file,String filename){
try{
FileOutputStream fos = new FileOutputStream(filename);
fos.write(file);
fos.close();
Messages.showSuccessfulSave();
}catch(Exception ex){
Messages.showFileSavingError(ex);
}
}
public void创建文件(int[]map){
byte[]file=新字节[fileLength];//22024192字节长
System.arraycopy(Sector.locationTable,0,file,0,Sector.locationTable.length);//这是正常工作的
对于(int cx=0;cx<16;cx++)
{
对于(int cz=0;cz<16;cz++)
{
int start=sectorLength+cx*(sectorLength*chunkSectorLength)+cz*(chunkRows*sectorLength*chunkSectorLength);//这个算法很有效,非常可怕
int[][]chunk=getChunk(map,cx*16,cz*16);//这是正常工作的
byte[]chunkdata=putChunk(chunk);//此字段中的数据是正确的
int计数器=0;
对于(int i=start;i为什么要将i
与chunkdata.length
进行比较,当i
用start
初始化时?我认为应该使用计数器
当前:
int counter = 0;
for(int i=start;i<chunkdata.length;i++){
file[i]=chunkdata[counter]; //Data loss here?
counter++;
}
int计数器=0;
对于(int i=start;iSorry,忘记提到它是数据丢失。例如,如果x=16和z=32,出于某种原因,它返回一个大的0数组。这看起来应该可以工作。你确定问题不在其他地方或数据中吗?老实说,不完全确定。我知道数据是正确的,因为我定期让它在数据丢失后打印小样本格式化完成。我在hole中尝试的是将数据分割成16x128x16x2的元素块,并将其处理为一个字节[]使用其他数据数组,然后将其保存到文件中。字节数组中的数据是正确的,但保存的数据都超过了第一个块的头0。我将使用除此之外最可能出现问题的部分更新帖子。非常感谢!在十六进制编辑器中检查文件后,它保存正确。我想我太忽略了该循环了注意到问题!很高兴知道它起作用了。有时你需要第二双眼睛来定位一个非常琐碎的问题:)
int counter = 0;
for(int i=start;counter<chunkdata.length;i++){
file[i]=chunkdata[counter]; //Data loss here?
counter++;
}
for(int i=start,counter = 0;counter<chunkdata.length;i++,counter++){
file[i]=chunkdata[counter]; //Data loss here?
}