It's not a secret anymore!
The moose likes I/O and Streams and the fly likes how to append to word doc files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "how to append to word doc files" Watch "how to append to word doc files" New topic

how to append to word doc files

Vaibhav Deshpande
Ranch Hand

Joined: Apr 26, 2002
Posts: 33

I have to microsoft word files. What I want to obtain through my application is to append content(data) of first file to second file.

my code is as follows. When i run this class a new micorsoft word file with name final get generated but it contains data and all binary data. so some problem may be with my code can some one help me out.



public class Appender {

// Returns the contents of the file in a byte array.
public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);

// Get the size of the file
long length = file.length();

// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.
if (length > Integer.MAX_VALUE) {
// File is too large

// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];

// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (, offset, bytes.length-offset)) >= 0) {
offset += numRead;

// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());

// Close the input stream and return bytes
return bytes;

public static void append(String fileName1, String fileName2) throws Exception
File f1 = new File(fileName1);
File f2 = new File(fileName2);

File finalFile = new File("C:/vebs/daemon/final.doc");

byte[] buffer1 = getBytesFromFile(f1);
byte[] buffer2 = getBytesFromFile(f2);

//BufferedOutputStream fw = new BufferedOutputStream (new FileOutputStream(finalFile, true));
FileOutputStream fw = new FileOutputStream (finalFile);

byte[] buffer3 = new byte[buffer1.length+buffer2.length];

System.out.println("buffer3.length = !" + buffer3.length + "!");

for(int i= 0 ; i < buffer1.length;i++) {
buffer3[i++]= buffer1[i];

int length = buffer1.length;

for(int i= 0 ; i < buffer2.length;i++) {
buffer3[length++]= buffer2[i];



public static void main(String[] args)

append("C:/vebs/daemon/to.doc", "C:/vebs/daemon/from.doc");
catch(FileNotFoundException fnfe)
System.out.println(" does not exist");
catch(Exception e)


David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Word documents are binary files with structure. You cannot manipulate them as easily as a text file. POI is an Apache project that might be able to help.
I agree. Here's the link:
subject: how to append to word doc files
It's not a secret anymore!