I'm using Java and I need to append, concat, merge or add, whichever is the right term, two .rtf files together in its original format for both rtf files, into one rtf file. Each of the rtf files are a page long, so I need to create a two page rtf file, from the two files.
I also need to create a page break between the two files, in the new combined rtf file. I went to MS word and was able to combine the two rtf files together, but that just created a long rtf file with no page break.
I have a code, but it only copies one file to another file in the same way, but I need help on tweaking out this code so I can copy two files into one file
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);
How do I add another FileInputStream object on top of the FileInputStream file, into FileOutputStream out, with a page break between file and object?
I am totally stuck. I was able to combine the two rtf files with help, but could not keep the original format of the two rtf file into the new one.
I tried the :
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(File file, boolean append), where the old.rtf is suppose to append to new.rtf, but when I do it, the old.rtf is just written into the new.rtf.
What am I doing wrong?
When you open the file you wish to add to, use the FileOutputStream(File file, boolean append)
with append
set to true, then you can add to the new file, rather than over write it.
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);
This will append old.rtf
to new.rtf
.
You can also do:
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);
That will concatenate old1.rtf
and old2.rtf
to the new file new.rtf
.