1

I just started with I/O in java and please excuse me if my doubt is silly but i find it dificult to understand how this code works? variable c in code below is not incremented then the while loop will never terminate even if there is only one character in inputstream and outputstream will be filled with only one character continuously but this code actually works satisfactorily which i dont understand why?

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyBytes {
public static void main(String[] args) throws IOException {

    FileInputStream in = null;
    FileOutputStream out = null;

    try {
        in = new FileInputStream("xanadu.txt");
        out = new FileOutputStream("outagain.txt");
        int c;

        while ((c = in.read()) != -1) {
            out.write(c);
        }
    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
}
}
Bhushan
  • 354
  • 9
  • 25
  • You got the same doubt like me ? :) [How the buffer byte array is continuously filling while streaming?](http://stackoverflow.com/questions/18105971/how-the-buffer-byte-array-is-continuously-filling-while-streaming) – Suresh Atta Oct 19 '13 at 07:15

4 Answers4

4

From the Java API: "A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment."

Your character c is being used to "read" bytes from the stream, when there are no more bytes left to read the read() method returns -1 and that will break the loop.

that's what

((c = in.read()) != -1)

Means, read until the value is -1.

pedromss
  • 2,443
  • 18
  • 24
2

When you use read () method, you actually move the position which you're reading every time until you get to the end of the file. So that's why it works.

Szymon
  • 42,577
  • 16
  • 96
  • 114
2

in.read() method read one character at a time. Each iteration one character is read and assigned to c.

Consider the following character's are present in the xanadu.txt file.

"ABCDE-1"

Note: -1 meaning of End of File (-1 is added for illustrate purpose only)

In first iteration

c=A while-> A!=-1 then true. then continue the while loop and save c='A' to outagain.txt file.

In Second iteration

c=B while -> B!=-1 then true. then continue the while loop and save c='B' to outagain.txt file.

In Third iteration

c=C while -> C!=-1 then true. then continue the while loop and save c='C' to outagain.txt file.

In fourth iteration

c=D while -> D!=-1 then true. then continue the while loop and save c='D' to outagain.txt file.

In Fifth iteration

c=E while -> E!=-1 then true. then continue the while loop and save c='E' to outagain.txt file.

In Sixth iteration

c=-1 while -> -1!=-1 then false. then exit from while loop

Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
0

The function read from a FileInputStream object, returns the next byte of data, or -1 if the end of the file is reached. (it actually returns an int)

Source: http://docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html#read%28%29

In your loop, you are doing: c = in.read() until c equals -1. So, at each iteration, c contains the next byte. read in doing the increment itself.

Maxime Chéramy
  • 17,761
  • 8
  • 54
  • 75