Java 将字节数组添加到另一个字节数组的特定位置

Java 将字节数组添加到另一个字节数组的特定位置,java,byte,Java,Byte,我想创建一个大小为512字节的字节数组 对于前100个字节,我想将其保留为filename,对于接下来的412个字节,我想使用它来存储文件本身的数据 大概是这样的: |--文件名为100字节--| |-----412字节文件数据------| byte[] buffer = new byte[512]; add the filename of byte[] type into the first 100 position insert the file da

我想创建一个大小为512字节的字节数组

对于前100个字节,我想将其保留为filename,对于接下来的412个字节,我想使用它来存储文件本身的数据

大概是这样的:

|--文件名为100字节--| |-----412字节文件数据------|

      byte[] buffer = new byte[512];
      add the filename of  byte[] type into the first 100 position
      insert the file data after the first 100 position 
文件名可以小于100字节

但我无法将文件数据附加到特定位置。。。我应该怎么做?

如何使用

您可能需要添加检查以确保data.length+name.length不超过512

要将名称的长度固定为100,请执行以下操作:

byte[] buffer = new byte[100 + name.length];
System.arraycopy(name, 0, buffer,   0, Math.min(100, name.length))
System.arraycopy(data, 0, buffer, 100, data.length)
要将总长度固定为512,请对data.length添加限制:

byte[] buffer = new byte[512];
System.arraycopy(name, 0, buffer,   0, Math.min(100, name.length))
System.arraycopy(data, 0, buffer, 100, Math.min(412, data.length))
使用如何

您可能需要添加检查以确保data.length+name.length不超过512

要将名称的长度固定为100,请执行以下操作:

byte[] buffer = new byte[100 + name.length];
System.arraycopy(name, 0, buffer,   0, Math.min(100, name.length))
System.arraycopy(data, 0, buffer, 100, data.length)
要将总长度固定为512,请对data.length添加限制:

byte[] buffer = new byte[512];
System.arraycopy(name, 0, buffer,   0, Math.min(100, name.length))
System.arraycopy(data, 0, buffer, 100, Math.min(412, data.length))
你可以用一个。它更容易阅读和遵循其他选项。如果需要,您还可以获得许多其他功能

byte[] buffer = new byte[512];
byte[] fileName = new byte[100];
byte[] data = new byte[412];

// Create a ByteBuffer from the byte[] you want to populate
ByteBuffer buf = ByteBuffer.wrap(buffer);

// Add the filename
buf.position(0);
buf.put(fileName);

// Add the file data
buf.position(99);
buf.put(data);

// Get out the newly populated byte[]
byte[] result = buf.array();
你可以用一个。它更容易阅读和遵循其他选项。如果需要,您还可以获得许多其他功能

byte[] buffer = new byte[512];
byte[] fileName = new byte[100];
byte[] data = new byte[412];

// Create a ByteBuffer from the byte[] you want to populate
ByteBuffer buf = ByteBuffer.wrap(buffer);

// Add the filename
buf.position(0);
buf.put(fileName);

// Add the file data
buf.position(99);
buf.put(data);

// Get out the newly populated byte[]
byte[] result = buf.array();
正如@Dorus所说,System.arraycopy对名称很有效,但文件数据可以直接读取到数组中:

    byte[] buffer = new byte[512];
    File file = new File("/path/to/file");
    byte[] fileNameBytes = file.getName().getBytes();
    System.arraycopy(fileNameBytes, 0, buffer, 0, fileNameBytes.length > 100 ? 100 : fileNameBytes.length);
    FileInputStream in = new FileInputStream(file); 
    in.read(buffer, 100, file.length() > 412 ? 412 : (int)file.length());
正如@Dorus所说,System.arraycopy对名称很有效,但文件数据可以直接读取到数组中:

    byte[] buffer = new byte[512];
    File file = new File("/path/to/file");
    byte[] fileNameBytes = file.getName().getBytes();
    System.arraycopy(fileNameBytes, 0, buffer, 0, fileNameBytes.length > 100 ? 100 : fileNameBytes.length);
    FileInputStream in = new FileInputStream(file); 
    in.read(buffer, 100, file.length() > 412 ? 412 : (int)file.length());

为什么你不能这样做呢?使用如何?我猜你的索引越界异常。尝试创建如下数组:byte[]newArr=newbyte[array1.length+array2.length];然后添加到newArr,显示您的代码将有助于解决很多问题,比如我为文件名添加了第一个字节数组6,然后为文件中的实际数据添加了412字节的数据。buffer.length将是418而不是512。您的评论与问题相矛盾。我已经编辑了我的答案,但我仍然感到困惑。如果为文件名添加6个字节,但由于前100个字节是为文件名保留的,因此跳过94个字节,则总共需要512个字节才能容纳412个字节的数据。文件名的长度是可变的,但最大为100字节,或者固定为100字节,其中一些字节可以保持未使用状态?为什么您不能这样做?使用如何?我猜您的索引越界异常。尝试创建如下数组:byte[]newArr=newbyte[array1.length+array2.length];然后添加到newArr,显示您的代码将有助于解决很多问题,比如我为文件名添加了第一个字节数组6,然后为文件中的实际数据添加了412字节的数据。buffer.length将是418而不是512。您的评论与问题相矛盾。我已经编辑了我的答案,但我仍然感到困惑。如果为文件名添加6个字节,但由于前100个字节是为文件名保留的,因此跳过94个字节,则总共需要512个字节才能容纳412个字节的数据。文件名的长度是可变的,但最大值是100字节,或者固定为100字节,其中一些字节可以保持未使用状态?我想为文件名保留前100字节,因此它应该是System.arraycopydata,0,buffer,100,data.length对吗?您可以用100 yes替换name.length的所有实例。您可以对name.length执行相同的操作,如果要将数组固定为512字节,请将其替换为412。如果DATA.LIGHT是可变的,则考虑使用Mth.Mim512、DATA长度+NAME.LATE和MUT.MI512-名称长度、数组大小的DATA长度和第二个ARARIAGE的最后一个参数。在第三个示例中。如果需要,可以将字节数组的大小减小为新的字节[100+Math.min412,data.length]。感谢您的努力!:我想为文件名保留前100个字节,因此它应该是System.arraycopydata,0,buffer,100,data.length,对吗?您可以将name.length的所有实例替换为100 yes。您可以对name.length执行相同的操作,如果要将数组固定为512字节,请将其替换为412。如果DATA.LIGHT是可变的,则考虑使用Mth.Mim512、DATA长度+NAME.LATE和MUT.MI512-名称长度、数组大小的DATA长度和第二个ARARIAGE的最后一个参数。在第三个示例中。如果需要,可以将字节数组的大小减小为新的字节[100+Math.min412,data.length]。感谢您的努力!: