【发布时间】:2019-07-26 07:14:16
【问题描述】:
List<byte[]> A has four byte[] values
当我为数组 A 运行 for each 循环并使用 writeByteArrayToFile(File,byte[],true); 写入文件时;
只是覆盖,最后一个byte[]值只写入文件?
我需要写所有字节[]???
【问题讨论】:
-
到目前为止你有什么尝试?
标签: java
List<byte[]> A has four byte[] values
当我为数组 A 运行 for each 循环并使用 writeByteArrayToFile(File,byte[],true); 写入文件时;
只是覆盖,最后一个byte[]值只写入文件?
我需要写所有字节[]???
【问题讨论】:
标签: java
解决方案:
public static void main(String[] args) throws IOException {
File A = new File("A");
File B = new File("B");
File C = new File("C");
File D = new File("D");
File E = new File("E");
mergeFiles(E, A, B, C, D);
}
static void mergeFiles(File destination, File... files) throws IOException {
byte[] buffer = new byte[512];
int count;
try (FileOutputStream fos = new FileOutputStream(destination)) {
for (File file : files) {
try (FileInputStream fis = new FileInputStream(file)) {
while ((count = fis.read(buffer)) != -1) {
fos.write(buffer, 0, count);
}
}
}
}
}
【讨论】:
read() 返回读入缓冲区的总字节数,如果由于已到达文件末尾而没有更多数据,则返回 -1。