IBM webMethods Hybrid Integration

IBM webMethods Hybrid Integration

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
  • 1.  Append one file to another

    Posted Wed August 01, 2012 03:24 AM

    Hello,

    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.


    #webMethods
    #Integration-Server-and-ESB
    #Flow-and-Java-services


  • 2.  RE: Append one file to another

    Posted Wed August 01, 2012 05:35 AM

    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.

    Senthil


    #webMethods
    #Integration-Server-and-ESB
    #Flow-and-Java-services


  • 3.  RE: Append one file to another

    Posted Thu August 02, 2012 05:07 PM

    Hi,

    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();
    else
    throw new ServiceException("appendFile: filename is missing");
    if (idc.first("data"))
    data = (String) idc.getValue();
    else
    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);
    buffer.write(data);
    buffer.close();
    writer.close();
    idc.destroy();
    } catch (Exception e) {
    throw new ServiceException(e);
    }

    #Integration-Server-and-ESB
    #Flow-and-Java-services
    #webMethods


  • 4.  RE: Append one file to another

    Posted Mon August 06, 2012 11:13 AM

    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.


    #Integration-Server-and-ESB
    #webMethods
    #Flow-and-Java-services