这些是自己零散的时间写的,比较乱
合并两个txt文件
我们先来看看最简单的使用合并流SequenceInputStream的方式进行操作
代码如下:
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
|
package
File;
/**
* 将两个txt文件合并为一个txt文件
* */
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.OutputStream;
import
java.io.SequenceInputStream;
public
class
FileDemo436{
public
static
void
main(String[] args)
throws
IOException{
File file1 =
new
File(
"d:"
+ File.separator +
"hello1.txt"
);
File file2 =
new
File(
"d:"
+ File.separator +
"hello2.txt"
);
File file3 =
new
File(
"d:"
+ File.separator +
"hello3.txt"
);
InputStream input1 =
new
FileInputStream(file1);
InputStream input2 =
new
FileInputStream(file2);
OutputStream output =
new
FileOutputStream(file3);
SequenceInputStream se =
new
SequenceInputStream(input1, input2);
int
temp =
0
;
while
((temp = se.read()) != -
1
){
output.write(temp);
}
input1.close();
input2.close();
output.close();
se.close();
}
}
|
读者可以自行测试,之后会发现在hello3.txt文件中包含了之前两个文件的全部内容。
对于文本文件完全正确,但是当我测试将上面的文本文件改为word的时候却出现错误,
笔者将上面代码部分的:
1
2
3
|
File file1 =
new
File(
"d:"
+ File.separator +
"hello1.txt"
);
File file2 =
new
File(
"d:"
+ File.separator +
"hello2.txt"
);
File file3 =
new
File(
"d:"
+ File.separator +
"hello3.txt"
);
|
改为:
1
2
3
|
File file1 =
new
File(
"d:"
+ File.separator +
"1.docx"
);
File file2 =
new
File(
"d:"
+ File.separator +
"2.docx"
);
File file3 =
new
File(
"d:"
+ File.separator +
"3.docx"
);
|
文件1.docx中内容为11111,文件2.docx中内容为22222,
但是程序产生的3.docx当我打开的时候出现:
当我单独将上面的:
File file3 =
new
File(
"d:"
+ File.separator +
"3.docx"
);
|
改为:
1
|
File file3 =
new
File(
"d:"
+ File.separator +
"3.txt"
);
|
时候,产生的txt文件内容为:
各种乱码,我不太清楚为什么错,不知道那位大牛知道,麻烦指教一下.
但是一般才有jacob操作这类办公软件,我以后会给出专门一篇文章,举例说明如何使用jacob。jacob也就是java-com桥,你可以在http://sourceforge.net/projects/jacob-project/下载
上面的例子合并的是2个txt文件,但是有时候需要合并很多的txt文件。
这个以合并3个txt文件为例子:
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
|
package
File;
/**
* 将两个txt文件合并为一个txt文件
* */
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.OutputStream;
import
java.io.SequenceInputStream;
import
java.util.Vector;
public
class
FileDemo436{
public
static
void
main(String[] args)
throws
IOException{
File file1 =
new
File(
"d:"
+ File.separator +
"1.txt"
);
File file2 =
new
File(
"d:"
+ File.separator +
"2.txt"
);
File file3 =
new
File(
"d:"
+ File.separator +
"3.txt"
);
File file4 =
new
File(
"d:"
+ File.separator +
"4.txt"
);
Vector<InputStream> vec =
new
Vector<InputStream>();
InputStream input1 =
new
FileInputStream(file1);
InputStream input2 =
new
FileInputStream(file2);
InputStream input3 =
new
FileInputStream(file3);
vec.add(input1);
vec.add(input2);
vec.add(input3);
OutputStream output =
new
FileOutputStream(file4);
SequenceInputStream se =
new
SequenceInputStream(vec.elements());
int
temp =
0
;
while
((temp = se.read()) != -
1
){
output.write(temp);
}
input1.close();
input2.close();
output.close();
se.close();
}
}
|
运行结果:
验证无误。
依照上面的例子的思想,当需要合并多个文件的时候,采用集合类将这些添加到集合中,最后使用合并流进行合并,当然也可以用最传统的办法,一个一个文件的读,边读边写。