Append one file to another


Any help would be greatly appreciated. What I am trying to accomplish would be to write a file to a directory which would already have a file in it. What i want to do is append to this final file and keep writing to it.

If you are in 8.x version, you can use the built-in service pub.file:stringToFile which can append your data to an existing file. For other version, you have to write custom java code to do the same. PSUtilities has a service for this I think.



To append data to a file in Java you can use FileWriter with the second parameter in the constructor set true. You can try this Java service to append a String to a file, if the file does not exists it is created.

try {
 IDataSharedCursor idc = pipeline.getSharedCursor();
 FileWriter writer = null;
 BufferedWriter buffer = null;
 String filename = null;
 String data = null;
 if (idc.first("filename"))
  filename = (String) idc.getValue();
  throw new ServiceException("appendFile: filename is missing");
 if (idc.first("data"))
  data = (String) idc.getValue();
  throw new ServiceException("appendFile: data is missing");
 // this is where you tell the FileWriter to append.
 writer = new FileWriter(filename, true);
 buffer = new BufferedWriter(writer);
} catch (Exception e) {
 throw new ServiceException(e);

PLEASE: Add a finally clause to the try/catch block to idc.destroy() the cursor. The service as coded above leaks cursor objects if it throws an exception.