本文目錄一覽:
Java如何高效合併多個文件
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”});
}
}
如何快速的合併pdf文件 java
將多個PDF文件合併到一起,首先需要有相關軟件的幫助,那就是迅捷pdf合併軟件。用戶電腦上下載安裝迅捷PDF合併軟件,運行迅捷PDF軟件,選擇將文件合併為PDF,然後就是添加文件到軟件中,支持多個PDF文件的批量轉換,點擊「添加文件」或者將文件直接拖拽到軟件中,添加完成後點擊右下角的「合併軟件」即可。用戶還可以選擇合併後的文件的保存路徑。只需稍等片刻,迅捷PDF合併軟件就可以完成多個文件的合併,合併完成後會自動保存到你所自定義的文件夾中。
方法二:在PDF文件里:文檔–插入頁面,進入要選擇插入的文件,選定後,選擇擬插入文件的位置,最後確定即可,非常方便。
方法二:使用pdfFactory軟件,到網上下載後,安裝到電腦上,這時在「打印機和傳真」里出現一個pdfFactory Pro的打印機。合併方法:1、首先將pdfFactory Pro的打印機設為默認打印機;2、選中需要合併的幾個文件,點右鍵打印,出現打印任務;3、在任務里調整合併文件的順序,最後保存PDF文件,合併文件結束。
如何使用java合併多個文件
使用java編程語言,對文件進行操作,合併多個文件,代碼如下:
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) {}
}
}
//下面代碼是將D盤的1.txt 2.txt 3.txt文件合併成out.txt文件。
public static void main(String[] args) {
mergeFiles(“D:/output.txt”, new String[]{“D:/1.txt”, “D:/2.txt”, “D:/3.txt”});
}
}
如何通過java將多個word文檔合成一個wor
國內有個免費的jar(Free Spire.Doc for Java),可用來合併Word文檔,分兩種合併方法:1.合併的內容新起一頁;2.合併的內容承接上文段落。
1.新起一頁合併
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class MergeWordDocument {
public static void main(String[] args){
//獲取第一個文檔的路徑
String filePath1 = “merge1.docx”;
//獲取第二個文檔的路徑
String filePath2 = “merge2.docx”;
//加載第一個文檔
Document document = new Document(filePath1);
//使用insertTextFromFile方法將第二個文檔的內容插入到第一個文檔
document.insertTextFromFile(filePath2, FileFormat.Docx_2013);
//保存文檔
document.saveToFile(“Output.docx”, FileFormat.Docx_2013);
}
}
2.承接上文段落合併
import com.spire.doc.Document;
import com.spire.doc.DocumentObject;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
public class MergeWordDocument {
public static void main(String[] args){
//獲取第一個文檔的路徑
String filePath1 = “merge1.docx”;
//獲取第二個文檔的路徑
String filePath2 = “merge2.docx”;
//加載第一個文檔
Document document1 = new Document(filePath1);
//加載第二個文檔
Document document2 = new Document(filePath2);
//獲取第一個文檔的最後一個section
Section lastSection = document1.getLastSection();
//將第二個文檔的段落作為新的段落添加到第一個文檔的最後一個section
for (Section section:(Iterable Section)document2.getSections()) {
for (DocumentObject obj:(Iterable DocumentObject)section.getBody().getChildObjects()
) {
lastSection.getBody().getChildObjects().add(obj.deepClone());
}
}
//保存文檔
document1.saveToFile(“Output.docx”, FileFormat.Docx_2013);
}
}
可參考原文。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/309537.html