File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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
Author

how to append to word doc files

Vaibhav Deshpande
Ranch Hand

Joined: Apr 26, 2002
Posts: 33
Hi,

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
is.close();
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];
}

fw.write(buffer3);

fw.close();
}


public static void main(String[] args)
{

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


thanks,

vebs
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: http://aspose.com/file-tools
 
subject: how to append to word doc files