使用java編程語(yǔ)言,對(duì)文件進(jìn)行操作,合并多個(gè)文件,代碼如下:
網(wǎng)站是企業(yè)的互聯(lián)網(wǎng)名片,是開(kāi)展互聯(lián)網(wǎng)業(yè)務(wù)基礎(chǔ)平臺(tái)。在目標(biāo)明確的基礎(chǔ)上,創(chuàng)新互聯(lián)建站憑借團(tuán)隊(duì)豐富的設(shè)計(jì)經(jīng)驗(yàn)完成網(wǎng)站的構(gòu)思創(chuàng)意即總體設(shè)計(jì)方案,自成立以來(lái),一直致力于為企業(yè)提供從國(guó)際域名空間、網(wǎng)站策劃、網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站制作、電子商務(wù)、成都外貿(mào)網(wǎng)站制作、網(wǎng)站推廣、網(wǎng)站優(yōu)化到為企業(yè)提供個(gè)性化軟件開(kāi)發(fā)等基于互聯(lián)網(wǎng)的全面整合營(yíng)銷(xiāo)服務(wù)。
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();
參考代碼如下:
public static void mergeFiles(String outFile, String[] files)
第一個(gè)參數(shù)是合并后生成文件的路徑
第二個(gè)參數(shù)是你需要合并的文本文件列表
代碼:
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)?{}
}
}
}
你 看 看 這 個(gè) ,是不是符合你的要求
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));
//輸入連續(xù)的個(gè)數(shù)
System.out.print("Input series string size:");
try {
int temp = Integer.parseInt(br.readLine());
res1txt = readFile(filepath1, temp);
res2txt = readFile(filepath2, temp);
//比較兩個(gè)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();
}
}
}
/**
* 讀取文件,把結(jié)果以輸入的連續(xù)個(gè)數(shù)分割,并保存在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;
}
}
這里發(fā)不了代碼,只能以文本的形式了
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));
//輸入連續(xù)的個(gè)數(shù)
System.out.print("Input series string size:");
try {
int temp = Integer.parseInt(br.readLine());
res1txt = readFile(filepath1, temp);
res2txt = readFile(filepath2, temp);
//比較兩個(gè)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();
}
}
}
/**
* 讀取文件,把結(jié)果以輸入的連續(xù)個(gè)數(shù)分割,并保存在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;
}
}