网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java合并文件的代码 java合并文件的代码有哪些

用java io流把多个txt文件的内容合并到一个文件里

参考代码如下:

成都创新互联公司2013年成立,先为榆中等服务建站,榆中等地企业,进行企业商务咨询服务。为榆中企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

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写的! 代码要写上!我用eclipse编译器!

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

public class Together {

/**

* @param files 需要合并的多个文件数组

* @param target 合并后保存的目标文件

*/

public void fileMerge(File files[], File target) {

final int BUFFER_SIZE = 0x500000;

try {

FileOutputStream fos = new FileOutputStream(target);

byte[] b = new byte[BUFFER_SIZE];

for (int i = 0; i files.length; i++) {

FileInputStream fis = new FileInputStream(files[i]);

while (fis.read(b) != -1) {

fos.write(b);

}

}

fos.flush();

System.out.println("合并success!");

} catch (Exception e) {

System.out.println("error: " + e);

}

}

}

如何使用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();


网站标题:java合并文件的代码 java合并文件的代码有哪些
标题网址:http://bjjierui.cn/article/dohspcs.html

其他资讯