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.


#TechXchangePresenter
 View Only
  • 1.  Easy method to count number of lines are in a file

    Posted Tue April 03, 2012 10:52 PM

    Just wondering if there’s an easy approach to count the total number of lines in a file without parsing it.


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


  • 2.  RE: Easy method to count number of lines are in a file

    Posted Wed April 04, 2012 02:56 PM

    Nope. Lines are defined by the presence of line terminators (CR, LF, or CR/LF). There is no way to count them other than reading the file contents. You can use Java IO classes to do this for you though.

    Unsolicited advice: In the “agreements” made with partners/systems, always explicitly specify the line terminators to be used in the files you are to process.


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


  • 3.  RE: Easy method to count number of lines are in a file

    Posted Wed April 04, 2012 03:02 PM

    Create a Java service with the input as the fileName. I have not tested this on large files but hope it will not break your IS.

    IDataCursor pipelineCursor = pipeline.getCursor();
    String    fileName = IDataUtil.getString( pipelineCursor, "fileName" );
    pipelineCursor.destroy();
    
    try {
    InputStream is;
    is = new BufferedInputStream(new FileInputStream(fileName));
    byte[] c = new byte[1024];
    int count = 0;
    int readChars = 0;
    while ((readChars = is.read(c)) != -1) {
    for (int i = 0; i < readChars; ++i) {
    if (c[i] == '\n')
    ++count;
    }
    }
    is.close();
    IDataUtil.put( pipelineCursor, "numberOfLines", count);
    } catch (IOException e) {
    e.printStackTrace();
    IDataUtil.put( pipelineCursor, "numberOfLines", e);
    }                 
    pipelineCursor.destroy();    
    }

    Cheers,
    Akshith


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


  • 4.  RE: Easy method to count number of lines are in a file

    Posted Wed April 04, 2012 03:04 PM

    Ah my bad, i did not pay attention to the not parsing the file requirement part. Ignore my previous post!


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


  • 5.  RE: Easy method to count number of lines are in a file

    Posted Fri April 06, 2012 06:55 PM

    I’d suggest using java.io.BufferedReader.readLine for line counting.


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