将字节数组添加到特定位置的另一个字节数组 java
Add byte array to another byte array at specific position java
我想创建一个大小为 512 字节的字节数组。
对于前 100 个字节,我想将它保留为文件名,对于接下来的 412 个字节,我想用它来存储文件本身的数据。
像这样:
|----100byte for file name ----||------------412 byte for file data------------|
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 字节。
但是我无法在特定位置追加文件数据...我该怎么办?
使用System.arraycopy()怎么样?
byte[] buffer = new byte[data.length + name.length];
System.arraycopy(name, 0, buffer, 0, name.length)
System.arraycopy(data, 0, buffer, name.length, 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))
您可以使用 ByteBuffer。它更容易阅读和遵循其他选项。如果以后需要,您还可以获得许多其他功能。
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());
我想创建一个大小为 512 字节的字节数组。
对于前 100 个字节,我想将它保留为文件名,对于接下来的 412 个字节,我想用它来存储文件本身的数据。
像这样:
|----100byte for file name ----||------------412 byte for file data------------|
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 字节。
但是我无法在特定位置追加文件数据...我该怎么办?
使用System.arraycopy()怎么样?
byte[] buffer = new byte[data.length + name.length];
System.arraycopy(name, 0, buffer, 0, name.length)
System.arraycopy(data, 0, buffer, name.length, 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))
您可以使用 ByteBuffer。它更容易阅读和遵循其他选项。如果以后需要,您还可以获得许多其他功能。
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();
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());