I am learning java from Java : Complete Reference.
I am currently working on the examples in the chapter : Input/Output : Exploring java.io
I dint understand few lines of code from the example below.
Can anyone help me with this example.
import java.io.*;
class FileInputStreamDemo
{
public static void main(String args[]) throws IOException
{
InputStream f = new FileInputStream("E://SomeRandomTextFile.txt");
System.out.println("Total available bytes : " + size = f.available());
int n = size/40;
System.out.println("First" + n + " bytes of file one read() at a time");
for(int i=0; i<n; i++)
{
System.out.println((char) f.read());
}
System.out.println("\n Still available: "+ f.available());
System.out.println("Reading the text " + n + " with one read(b[])");
byte b[] = new byte[n];
if(f.read(b) != n)
{
System.err.println("coudn't read" + n + "bytes.");
}
System.out.println(new String(b,0,n));
}
In the above code, I dint understand the last five lines of code.
What is the outcome of
f.read(b)
What is
System.err
and
What is the outcome of
new String(b,0,n);
Should be:
It’s a method call to read bytes from the file into the buffer. From javadoc:
This line:
creates a new
Stringfrom the bytes inside the bufferb, starting from index 0 and taking the next n bytes. From javadoc:And finally this:
returns a reference to the program standard error stream.