-4

I am quite new to programming.

While reading the article Byte Streams in "Basic I/O" in The Java Tutorials by Oracle, I came accross this code:

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();
            }
        }
    }
}

I do not understand the condition of the while-loop. Is -1 some kind of sign that the Message is over? Does the FileOutputStream add it at the end?

Thank you all for your attention. I hope you have a wonderfull sylvester.

Basil Bourque
  • 218,480
  • 72
  • 657
  • 915
Auri
  • 1
  • 3
    See [docs](https://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html#read--) — "-1 if the end of the stream is reached" – khelwood Dec 30 '20 at 16:17
  • Thank you very much. – Auri Dec 30 '20 at 16:19
  • What's a 'sylvester', by the way? This is not a common expression in English. 'New Year', perhaps? – a guest Dec 30 '20 at 16:34
  • @aguest It is the last day of the year, and it is spelled Silvester. – Henry Dec 30 '20 at 16:37
  • Yes, I meant new year. Its "Silvester" in my mothertongue and I thought it is the same in English, because it is the name of a Saint and names do not change that much between languages. – Auri Dec 30 '20 at 16:39
  • Much to my surprise, (secular) New Year's Eve in Israel is also "Silvester's Day". Apparently his birthday was December 31. – Frank Yellin Dec 30 '20 at 16:52

2 Answers2

2

To add to the other answers, the tool for figuring this out is the documentation.

For the 'read' method of FileInputStream:

public int read()
throws IOException

Reads a byte of data from this input stream. This method blocks if no input is yet available. Specified by: read in class InputStream

Returns: the next byte of data, or -1 if the end of the file is reached.

This is definitive.

All standard Java classes are documented in this manner. In case of uncertainty, a quick check will reassure you.

a guest
  • 442
  • 1
  • 5
-3

EDIT: "Signals that an end of file or end of stream has been reached unexpectedly during input. This exception is mainly used by data input streams, which generally expect a binary file in a specific format, and for which an end of stream is an unusual condition. Most other input streams return a special value on end of stream."

The right way is to catch EOFException to find out is it end of file or not, but in tihs case reading chars as EOF -1 is returned and not null, and it's working because there is no char for negative ascii, it's the same to check while ((c = in.read()) >= 0) {}, so you can use != -1 and it will work.