我正在使用Java,我需要将两个.rtf文件以其原始格式(对于两个rtf文件)一起附加,合并,合并或添加(以正确的术语为准)到一个rtf文件中。每个rtf文件都有一页,因此我需要从这两个文件中创建一个两页的rtf文件。
我还需要在新的组合rtf文件中的两个文件之间创建一个分页符。我读了MS Word,能够将两个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。
我究竟做错了什么?
当您打开要添加到的文件时,将FileOutputStream(File file, boolean append)
withappend
设置为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.rtf
到new.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.rtf
并连接old2.rtf
到新文件new.rtf
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句