新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
参考代码如下:
成都创新互联公司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) {}
}
}
}
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编程语言,对文件进行操作,合并多个文件,代码如下:
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();