Hi all,
I am trying to understand as to how this InputStream.read(byte[] b) function actually works.
I have a doubt regarding the InputStream.read(byte[] b) function.
The code is as follows:-

import java.io.*;

public class SpeedReading {

	public static int count(String filename) throws IOException {
		InputStream is = new BufferedInputStream(new FileInputStream(filename));    
		byte[] c = new byte[1024]; 
		int count = 0;     
		int readChars = 0; 
		
		[B]//readChars=is.read(c);[/B]
		
		
		while ((readChars = is.read(c)) != -1) 
		{
			for (int i = 0; i < readChars; ++i) 
			{             
				System.out.println(c[i]);
				if (c[i] == '\n')                 
					++count;         
			}
		 }     
		return count; 
		
	}
	

	public static void main(String[] args)
	{
		try
		{
		    System.out.println(count("SpeedRead.txt"));
		}catch(IOException e){
			System.out.println(e);
		}
	}
}

The text(SpeedRead.txt) file contains the following text:
HELLO

Now my doubt is as to how this read(byte[] b) function actually behaves.
In the commented line that is marked as bold, if I execute this program, the control does not go into the while loop. Why does this happen since readchars is anyways taking value in the while loop argument.
On the other hand, if i remove this line, the program works perfectly.
I am confused !! :(
Also are there any limitations on using this read(byte[] b) function to only as arguments
to the loop statements.If yes, please brief me over this.

With the commented line in you read the whole file into the buffer. Then in the while you start by reading again - but the file has now been read so you get an immediate end-of-file and the var is et to -1.
read(byte[]) works just fine.

With the commented line in you read the whole file into the buffer. Then in the while you start by reading again - but the file has now been read so you get an immediate end-of-file and the var is et to -1.
read(byte[]) works just fine.

thnx a lot for your reply. !! :)

This question has already been answered. Start a new discussion instead.