2016-12-19 96 views
0

我正在使用Java,我需要将两个.rtf文件以其原始格式(包括两个rtf文件)一起追加,连续编译,合并或添加,无论哪个是正确的术语,放入一个rtf文件。每个rtf文件都是一个页面,所以我需要从两个文件中创建一个两页的rtf文件。合并RTF文件?

我还需要在新组合的rtf文件中创建两个文件之间的分页符。我去了MS的话,并能够将两个rtf文件结合在一起,但是这只是创建了一个没有分页符的长rtf文件。

我有一个代码,但它只是将文件复制到相同的方式另一个文件,但我需要调整出这个代码的帮助,所以我可以两个文件复制到一个文件

FileInputStream file = new FileInputStream("old.rtf"); 
    FileOutputStream out = new FileOutputStream("new.rtf"); 

    byte[] buffer = new byte[1024]; 

    int count; 

    while ((count= file.read(buffer)) > 0) 
     out.write(buffer, 0, count); 

怎么办我在FileInputStream文件的顶部添加另一个FileInputStream对象,FileOutputStream输出,文件和对象之间有分页符?

我完全卡住了。我能够将两个rtf文件与帮助相结合,但无法将两个rtf文件的原始格式保留为新格式。

我试过:

FileInputStream file = new FileInputStream("old.rtf"); 
    FileOutputStream out = new FileOutputStream("new.rtf", true); 

    byte[] buffer = new byte[1024]; 

    int count; 
    while ((count= file.read(buffer)) > 0) 
    out.write(buffer, 0, count); 

FileOutputStream中(文件文件,布尔追加),其中old.rtf是假设追加到new.rtf,但是当我做吧,old.rtf只是写入new.rtf。

我在做什么错?

回答

0

当您打开要添加到的文件时,请使用FileOutputStream(File file, boolean append)并将append设置为true,然后您可以将其添加到新文件中,而不是将其写入。

FileInputStream file = new FileInputStream("old.rtf"); 
FileOutputStream out = new FileOutputStream("new.rtf", true); 

byte[] buffer = new byte[1024]; 

int count; 

while ((count= file.read(buffer)) > 0) 
    out.write(buffer, 0, count); 

这将追加到old.rtfnew.rtf

你也可以这样做:

FileInputStream file = new FileInputStream("old1.rtf"); 
FileOutputStream out = new FileOutputStream("new.rtf"); 

byte[] buffer = new byte[1024]; 

int count; 

while ((count= file.read(buffer)) > 0) 
    out.write(buffer, 0, count); 

file.close(); 

file = new FileOutputStream("old2.rtf"); 
while ((count= file.read(buffer)) > 0) 
    out.write(buffer, 0, count); 

这将串联old1.rtfold2.rtf到新文件new.rtf

+0

有什么方法可以阐述答案。这有点合理,但仍然很失败。谢谢 – user2852918

+0

Will Hartung,我真的很感谢你的回应,并且知道它的正确性,但我无法理解它。我试图把它放进去,并不适合我。我究竟做错了什么??我对这个太新了,只是没有得到它。 – user2852918