javatxt合并的简单介绍

博主:adminadmin 2022-11-21 20:11:07 107

今天给各位分享javatxt合并的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

用java实现合并多个txt文件

可以试下用BufferedReader和BufferedWriter进行文件读写,合并在读的时候做就行了。

如何使用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合并两个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 io流把多个txt文件的内容合并到一个文件里

参考代码如下:

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

第一个参数是合并后生成文件的路径

第二个参数是你需要合并的文本文件列表

代码:

package org.lq.util;

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.CharBuffer;

import java.nio.channels.FileChannel;

import java.nio.charset.Charset;

import java.nio.charset.CharsetDecoder;

import java.nio.charset.CharsetEncoder;

import java.util.Arrays;

public class MergeFile {

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){

Charset charset=Charset.forName("utf-8");

CharsetDecoder chdecoder=charset.newDecoder();

CharsetEncoder chencoder=charset.newEncoder();

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

ByteBuffer bb = ByteBuffer.allocate(BUFSIZE);

CharBuffer charBuffer=chdecoder.decode(bb);

ByteBuffer nbuBuffer=chencoder.encode(charBuffer);

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

bb.flip();

nbuBuffer.flip();

outChannel.write(nbuBuffer);

    bb.clear();

    nbuBuffer.clear();

}

fc.close();

}

out.println("Merged!! ");

} catch (IOException ioe) {

ioe.printStackTrace();

} finally {

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

}

}

}

java如何合并多个大的txt文件?(每个文件50M)。nio处理文件如何提高速度?

这种情况java.io, nio没有大区别

byte[] buf = new byte[8 * 1024];

try (OutputStream out = new new FileOutputStream(outfile)) {

    for (File f : txtFiles) {

        try (FileInputStream in = new FileInputStream(f)) {

           org.apache.commons.io.IOUtils.copyLarge(in, out, buf);

        }

    }

}

要是linux下,shell里直接执行cat *.txt out.txt就可以,不用写代码

JAVA合并TXT文件数据

ok!我给你写完了,首先你在D盘下面见两个文件

分别是 file1.txt和file2.txt

如下所示

file1.txt

-----------------------------

id|name|class

1|test|class1

2|test2|class2

3|test3|class3

file2.txt

-----------------------------------

id|sex|teacher

1|male|wang

2|female|zhang

3|male|li

------------------------------------------

程序如下:

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

public class CombinFile {

private final String FILE1 = "d:\\file1.txt";

private final String FILE2 = "d:\\file2.txt";

private final String FILENAME="d:\\combinFile.txt";

private InputStream getFileStream(String fileName) throws FileNotFoundException{

FileInputStream fis = new FileInputStream(new File(fileName));

return fis;

}

private void combinFile() throws IOException{

FileOutputStream fos = new FileOutputStream(new File(FILENAME));

InputStream fis1 = getFileStream(FILE1);

InputStream fis2 = getFileStream(FILE2);

int len = 0;

while((len=fis1.read())!=-1){

fos.write(len);

}

System.out.println("第一个文件已经写到目标文件.....");

fis1.close();

fos.flush();

while((len=fis2.read())!=-1){

fos.write(len);

}

System.out.println("第二个文件已经写到目标文件");

fis2.close();

fos.flush();

fos.close();

System.out.println("文件整合完毕");

}

public static void main(String[] args) {

try {

new CombinFile().combinFile();

} catch (IOException e) {

e.printStackTrace();

}

}

}

--------------------------------------------------------

程序运行如下:

产生combinFile.txt文件,内容如下

id|name|class

1|test|class1

2|test2|class2

3|test3|class3

id|sex|teacher

1|male|wang

2|female|zhang

3|male|li

javatxt合并的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、javatxt合并的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-21,除非注明,否则均为首码项目网原创文章,转载请注明出处。