views:

100

answers:

4

While find out no of line in a file, it displays more number of lines.

for example in my file having only 26 line in word document. But while count using java program it displays 118.

    File f=new File("C:\\Users\\os05\\Desktop\\Venkatesan(13-02-10).doc");

    FileReader fr = new FileReader(f);

    LineNumberReader ln = new LineNumberReader(fr);

    int count = 0;

    while (ln.readLine() != null)
    {

      count++;
    }
    System.out.println("No of lines:"+count);

The above code, how is calculate the no. of line....?

+3  A: 

If it is Microsoft Word Document, they are binary files, you couldn't do that way.

You need to find appropriate api for microsoft word files.

S.Mark
+1 for your patience
helios
+2  A: 

Your problem is that you are looking at a doc file - which is not held in plain text. In order to find the number of lines in a microsoft word file, you are going to have to use a dedicated library...

The file format is available at www.wotsit.org, but I doubt that this alone will help you...

Martin Milan
+1 for your patience
helios
+10  A: 

You're trying to treat a Word document as if it were a plain text file (*).

A Word document however is a binary file with a proprietary format that you need to interpret correctly to extract the information contained in it.

There are libraries out there that handle such files, for example Apache POI.

If you just want to do this for experimentation and learning, then it might be easier to just stick with simple text files (as produced by Notepad, for example).

(*) even if there is no such thing as plain text.

Joachim Sauer
+1 for your patience and the Unicode article reference
helios
A: 

You may also use the open office api to access to contents of Office documents. FAQ on OpenOffice.org API

elou