Error while counting number of character,lines and words in java
<p>i have written the following code to count the number of character excluding white spaces,count number of words,count number of lines.But my code is not showing proper output.</p>

<pre><code>import java.io.*;

 class FileCount
{


public static void main(String args[]) throws Exception
{
    FileInputStream file=new FileInputStream("sample.txt");
    BufferedReader br=new BufferedReader(new InputStreamReader(file));
    int i;
    int countw=0,countl=0,countc=0;
    do
    {
        i=br.read();
        if((char)i==(' '))
            countw++;
        else if((char)i==('\n'))
            countl++;
        else 
            countc++;



    }while(i!=-1);
    System.out.println("Number of words:"+countw);
    System.out.println("Number of lines:"+countl);
    System.out.println("Number of characters:"+countc);
}
}
</code></pre>

<p>my file sample.txt has </p>

<pre><code>hi my name is john
hey whts up
</code></pre>

<p>and my out put is </p>

<pre><code>Number of words:6
Number of lines:2
Number of characters:26
</code></pre>
