• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

how to append to word doc files

Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

package com.wow.framework.proploader.javafiles;

import java.io.*;

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
&& (numRead=is.read(bytes, 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)


Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
    Bookmark Topic Watch Topic
  • New Topic