java合并2个txt文本,Java实现多个文档合并输出到一个文档

本文实例为大家分享了Java实现多个文档合并输出到一个文档的具体代码,供大家参考,具体内容如下

方法:Java NIO

package First;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.nio.channels.FileChannel;

import java.nio.channels.WritableByteChannel;

public class Test {

public static void main(String params[]) throws Exception {

String[] iF = new String[] {"E:/test1.txt", "E:/test2.txt", "E:/test3.txt", "E:/test4.txt" };

String oF = "E:/out.txt";

FileOutputStream output = new FileOutputStream(new File(oF));

WritableByteChannel targetChannel = output.getChannel();

for(int i =0; i

FileInputStream input = new FileInputStream(iF[i]);

FileChannel inputChannel = input.getChannel();

inputChannel.transferTo(0, inputChannel.size(), targetChannel);

inputChannel.close();

input.close();

}

targetChannel.close();

output.close();

System.out.println("All jobs done...");

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持聚米学院。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了合并多个docx文件一个大的docx文件,你可以使用 Apache POI 库。 Apache POI 是一个开源的 Java 库,可以用于读写 Microsoft Office 文件格式。你可以使用它来读取你想要合并的所有docx文件,然后将它们写入一个新的docx文件中。 具体来说,你可以使用 XWPFDocument 类来读取每一个docx文件,然后使用 createParagraph() 方法来创建段落,使用 createRun() 方法来创建文本块,并使用 setText() 方法来设置文本。最后,你可以使用 write() 方法将新的docx文件写入磁盘。 下面是一个示例代码,它将多个docx文件合并一个新的docx文件中: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.List; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; public class MergeDocx { public static void main(String[] args) throws Exception { // 创建新的docx文件 XWPFDocument newDoc = new XWPFDocument(); FileOutputStream out = new FileOutputStream("merged.docx"); // 读取要合并的所有docx文件 for (int i = 1; i <= 3; i++) { InputStream in = new FileInputStream("document" + i + ".docx"); XWPFDocument doc = new XWPFDocument(in); // 遍历所有段落 List<XWPFParagraph> paragraphs = doc.getParagraphs(); for (XWPFParagraph paragraph : paragraphs) { // 复制段落 XWPFParagraph newPara = newDoc.createParagraph(); newPara.getCTP().set(paragraph.getCTP()); // 遍

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值