java合併文件,java文件拆分與合併

本文目錄一覽:

java合併兩個txt文件並生成新txt

import java.io.File;

import java.io.PrintStream;

import java.util.Scanner;

/**

 * 2015年11月18日上午9:31:05

 * 

 * @author cs2110 TODO 合併數組

 *

 */

public class MergeFile {

    private String afile = “D:/1.txt”;

    private String bfile = “D:/2.txt”;

    private String mergefile = “D:/3.txt”;

    /**

     * 讀取文件裡面的整數

     * 

     * @param input

     *            Scanner對象

     * @return 返回整形數組

     */

    public int[] readFile(Scanner input) {

        try {

            String temp = “”;

            while (input.hasNextInt()) {

                temp += input.nextInt() + “,”;

            }

            String[] nums = temp.split(“,”);

            int[] arr = new int[nums.length];

            for (int index = 0; index  nums.length; index++) {

                arr[index] = Integer.parseInt(nums[index]);

            }

            return arr;

        } catch (Exception e) {

            e.printStackTrace();

        }

        return null;

    }

    /**

     * 合併數組

     * 

     * @param a

     *            數組1

     * @param b

     *            數組2

     * @return 整形數組

     */

    public int[] merge(int[] a, int[] b) {

        int len = a.length;

        if (b.length  len) {

            len = b.length;

        }

        int[] all = new int[a.length + b.length];

        int index = 0;

        int aIndex = 0;

        int bIndex = 0;

        while (aIndex  len || bIndex  len) {

            if (a[aIndex]  b[bIndex]) {

                all[index] = a[aIndex];

                aIndex++;

            } else {

                all[index] = b[bIndex];

                bIndex++;

            }

            index++;

        }

        if (aIndex  a.length) {

            while (aIndex  a.length) {

                all[index++] = a[aIndex++];

            }

        } else {

            while (bIndex  b.length) {

                all[index++] = b[bIndex++];

            }

        }

        return all;

    }

    /**

     * 寫入文件

     * 

     * @param print

     *            PrintStream

     * @param a

     *            數組

     */

    public void writeFile(PrintStream print, int[] a) {

        for (int index = 0; null != a  index  a.length; index++) {

            print.append(a[index] + “\r\n”);

        }

    }

    public static void main(String[] args) {

        MergeFile merge = new MergeFile();

        if (null != args  args.length  2) {// 輸入參數合法,則使用,否則按照默認

            merge.afile = args[0];

            merge.bfile = args[1];

            merge.mergefile = args[2];

        } else {

            System.out.println(“Using the default file”);

        }

        Scanner input = null;

        int[] a = null;

        int[] b = null;

        int[] all = null;

        try {

            input = new Scanner(new File(merge.afile));

            a = merge.readFile(input);

            input = new Scanner(new File(merge.bfile));

            b = merge.readFile(input);

            all = merge.merge(a, b);

            PrintStream print = new PrintStream(new File(merge.mergefile));

            merge.writeFile(print, all);

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

如何通過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);

    }

}

可參考原文。

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”});

}

}

如何使用Java合併多個文件

使用java編程語言,對文件進行操作,合併多個文件,代碼如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

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();

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/227385.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-09 16:29
下一篇 2024-12-09 16:29

相關推薦

發表回復

登錄後才能評論