Easy method to count number of lines are in a file

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

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.

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

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

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