文件操作的一些疑问

简介:

这些是自己零散的时间写的,比较乱

合并两个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操作这类办公软件,我以后会给出专门一篇文章,举例说明如何使用jacobjacob也就是java-com桥,你可以在http://sourceforge.net/projects/jacob-project/下载

 

上面的例子合并的是2txt文件,但是有时候需要合并很多的txt文件。

这个以合并3txt文件为例子:

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();
     }
}

运行结果:

验证无误。

依照上面的例子的思想,当需要合并多个文件的时候,采用集合类将这些添加到集合中,最后使用合并流进行合并,当然也可以用最传统的办法,一个一个文件的读,边读边写。

目录
相关文章
|
10月前
|
存储 Linux Serverless
C进阶:文件操作
C进阶:文件操作
|
存储 C语言
文件操作的全部注意过程
文件操作的全部注意过程
|
10月前
|
存储 Python
文件操作详解与实战
文件操作详解与实战
|
10月前
|
存储 Windows
学习文件和文件操作
要将数据进⾏持久化的保存,我们可以使用文件。
70 0
|
索引
简单粗暴的实现一下:文件操作!
简单粗暴的实现一下:文件操作!
52 0
|
存储 C语言
C语言进阶之文件操作及改造通讯录(下)
如果要求在外存上以ASCII码的形式存储,则需要在存储前转换。以ASCII字符的形式存储的文件就是文本文件。
|
存储 C++ iOS开发
【C++知识点】文件操作(一)
【C++知识点】文件操作(一)
114 0
|
iOS开发 C++
【C++知识点】文件操作(二)
【C++知识点】文件操作(二)
112 0
|
存储 C语言
文件操作【下篇】
文件操作【下篇】
96 0