java多文件代码合并 java多文件编程
如何使用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 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 中IO流操作字符串 两个文件中有一些相同的代码 现在要将两个合并成一个 并且去除相同代码 祥看补充
你 看 看 这 个 ,是不是符合你的要求
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class TestOne {
/**
* @param args
*/
public static void main(String[] args) {
String filepath1 = "F:\\1.txt";
String filepath2 = "F:\\2.txt";
ListString res1txt = null;
ListString res2txt = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//输入连续的个数
System.out.print("Input series string size:");
try {
int temp = Integer.parseInt(br.readLine());
res1txt = readFile(filepath1, temp);
res2txt = readFile(filepath2, temp);
//比较两个list中的相同值
for (int i = 0; i res1txt.size(); i++) {
for (int j = 0; j res2txt.size(); j++) {
if(res1txt.get(i).equals(res2txt.get(j))){
System.out.println(res1txt.get(i));
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 读取文件,把结果以输入的连续个数分割,并保存在List中
* @param filepath
* @param size
* @return
*/
public static ListString readFile(String filepath, int size){
//如果size不合法,则返回空
if(size = 0)
return null;
ListString list = new ArrayListString();
BufferedReader br = null;
try {
//读文件
br = new BufferedReader(new InputStreamReader(new FileInputStream(filepath)));
String temp = null;
while((temp = br.readLine()) != null){
for (int i = 0; i temp.length() - size; i++) {
list.add(temp.substring(i, i+size));
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
}
这里发不了代码,只能以文本的形式了
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class TestOne {
/**
* @param args
*/
public static void main(String[] args) {
String filepath1 = "F:\\1.txt";
String filepath2 = "F:\\2.txt";
ListString res1txt = null;
ListString res2txt = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//输入连续的个数
System.out.print("Input series string size:");
try {
int temp = Integer.parseInt(br.readLine());
res1txt = readFile(filepath1, temp);
res2txt = readFile(filepath2, temp);
//比较两个list中的相同值
for (int i = 0; i res1txt.size(); i++) {
for (int j = 0; j res2txt.size(); j++) {
if(res1txt.get(i).equals(res2txt.get(j))){
System.out.println(res1txt.get(i));
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 读取文件,把结果以输入的连续个数分割,并保存在List中
* @param filepath
* @param size
* @return
*/
public static ListString readFile(String filepath, int size){
//如果size不合法,则返回空
if(size = 0)
return null;
ListString list = new ArrayListString();
BufferedReader br = null;
try {
//读文件
br = new BufferedReader(new InputStreamReader(new FileInputStream(filepath)));
String temp = null;
while((temp = br.readLine()) != null){
for (int i = 0; i temp.length() - size; i++) {
list.add(temp.substring(i, i+size));
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
}
网页名称:java多文件代码合并 java多文件编程
新闻来源:http://hbruida.cn/article/dogejjj.html