2

Why don't we need i++ in the following snippet:

import java.io.*;
    class PracIn
    {
        public static void main(String args[])
        {
            try
            {
            System.out.println("Reading From File.....\n\n");

            /* You MUST create First File myOwnFile.txt in your machine */
            /* Add some Text on that File */

            FileInputStream fin=new FileInputStream("myOwnFile.txt");   

            int i=0;

            while((i=fin.read())!=-1)
            {
                System.out.print((char)i);      /* why we don't need i++*/
            }
            }
            catch(Exception e)
            {
            }
        }
    }

What is the theory behind the logic?

GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
  • fin.read() fetches next byte from file. As, its reaches to end, it will return -1 as it doesn't have any thing to read, i.e. condition to check for end of file And loops terminates – Ashish Ani Dec 31 '15 at 10:05

4 Answers4

3

i is not a loop index here, so there's no need to increment it. i is assigned each byte being read from the file.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

The read() method basically returns the "the next byte of data, or -1 if the end of the file is reached" and it is assigned to i. The while loop is checking the value of i as a conditional statement. So that's why we don't need to increment the value of i (the value of i depends on return value from read () function of FileInputStream). The loop will be ended once the the condition will be true; which means the value of i will be -1 (end of the file reached).

For details clarification, please go through the below link. Here you can find the details of the read() method. Hope this answers your question.

https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html

Koushik
  • 11
  • 2
1

this is the code snippet that i have used to read line from file and store it in a string array, hope this will be useful for you :)

public class user {
 public static void main(String x[]) throws IOException{
  BufferedReader b=new BufferedReader(new FileReader("<path to file>"));
  String[] user=new String[30];
  String line="";
  while ((line = b.readLine()) != null) {
   user[i]=line; 
   System.out.println(user[1]);
   i++;  
   }

 }
}
NikhilP
  • 1,508
  • 14
  • 23
0

Why do you think that you need to do i++.

int i;
i=fin.read();

Here you reading a character at a time. Also, it is not like a pointer in the file that you need to increment each time you read a character. The read() method read the character and returns it. When it encounter the end of the file, it will return the -1. Because while reading each of the character is returned by their ASCII value, so you need to convert back to char and print it.


Here is the implementation of read() method from FileInputStream class. It uses a native implementation for reading the byte from the file.

public int read() throws IOException {
    return read0();
}

private native int read0() throws IOException;

Further reading about native read(): native read()

Community
  • 1
  • 1
YoungHobbit
  • 13,254
  • 9
  • 50
  • 73