我是靠谱客的博主 追寻小蝴蝶,这篇文章主要介绍java 合并多个文件_java中如何将两个文件合并到另一个文件,现在分享给大家,希望可以做个参考。

展开全部

java可以使用FileChannel快速高效地将多个文件合并到一起62616964757a686964616fe58685e5aeb931333337396235,以下是详细代码:import static java.lang.System.out;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.nio.ByteBuffer;

import java.nio.channels.FileChannel;

import java.util.Arrays;

public class test {

public static final int BUFSIZE = 1024 * 8;

public static void mergeFiles(String outFile, String[] files) {

FileChannel outChannel = null;

out.println("Merge " + Arrays.toString(files) + " into " + outFile);

try {

outChannel = new FileOutputStream(outFile).getChannel();

for(String f : files){

FileChannel fc = new FileInputStream(f).getChannel();

ByteBuffer bb = ByteBuffer.allocate(BUFSIZE);

while(fc.read(bb) != -1){

bb.flip();

outChannel.write(bb);

bb.clear();

}

fc.close();

}

out.println("Merged!! ");

} catch (IOException ioe) {

ioe.printStackTrace();

} finally {

try {if (outChannel != null) {outChannel.close();}} catch (IOException ignore) {}

}

}

public static void main(String[] args) {

mergeFiles("D:/output.txt", new String[]{"D:/in_1.txt", "D:/in_2.txt", "D:/in_3.txt"});

}

}

最后

以上就是追寻小蝴蝶最近收集整理的关于java 合并多个文件_java中如何将两个文件合并到另一个文件的全部内容,更多相关java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(191)

评论列表共有 0 条评论

立即
投稿
返回
顶部