Identify EOF in a Datainputstream

Asked

Viewed 197 times

1

I am making a program that needs to read a binary file and go extracting some information from it, what I have so far is the following:

public void processarArquivo() throws Exception{

    try {
        FileInputStream fileInputStream = new FileInputStream(ARQUIVO DE ENTRADA);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        DataInputStream objectIn = new DataInputStream(bufferedInputStream);

        while (objectIn != null)
        {
            try {
                String data = objectIn.readUTF();
                char[]direcaoVento = objectIn.readChar();
                int velocidadeVento = objectIn.readInt();
                int indicePluviometrico = objectIn.readInt();
                float temperatura = objectIn.readFloat();
            }
            catch(IOException e){
                objectIn.close();
                objectIn = null;
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

The mirror in my file is this:

Espelho do arquivo DadosMeteorologicos-Exemplo.dat
10/10/2015-E -1-15-19.8
11/10/2015-SE-38-16-15.1
12/10/2015-NW-69-4-15.6
13/10/2015-W -9-3-18.1
14/10/2015-NE-11-14-27.8
15/10/2015-SW-51-0-28.7
16/10/2015-NW-24-0-17.8
17/10/2015-E -11-12-16.1
18/10/2015-E -35-0-26.2
19/10/2015-W -42-8-15.8
20/10/2015-SE-14-17-21.7
21/10/2015-NW-51-0-26.0
22/10/2015-E -37-0-25.2
23/10/2015-SW-9-15-26.1
24/10/2015-NE-2-16-21.9

My question refers to while, is there any way to verify when I arrive at the end of the objectIn? Currently I simply have one while (true) and wait for the program to fire a Exception. But it seems wrong to me anyway.

  • 1

    Looks like it’s just capturing one EOFException while(true) same. Maybe if you add how the data is in that input file, we can suggest another alternative solution.

  • @Articuno That’s how I managed to do it, but you agree that it seems "wrong"?

  • If the documentation suggests, how can it be wrong? Adds a sample of the file in question if you want another read option.

  • I hadn’t really seen that the documentation itself suggested this

  • How is this file? This mirror gives the impression that it is textual, with a variable number of lines and with fields separated by hyphen. Is it like that or is it binary? If it’s like I said, it’s easier to read line by line and break each one with a String.split(), then make the appropriate conversions from each field. It seems to me that the DataInputStream is more for when you know the amount of information you want to extract from the file (usually binary or mixed).

  • @Suspect that it is not the intention of the documentation to suggest using the EOFException. The case is that a UTF character can be composed of more than one byte and a EOFException exists to indicate that the reading of bytes expected to find say two bytes but was interrupted in the middle by a EOF.

  • The file is binary, the mirror is just trying to expose what is the content to facilitate in understanding the code

Show 2 more comments

1 answer

1

That’s how I managed to do it, but you agree that it feels "wrong"?

Yes, looks like a little wrong. At least by documentation of DataInputStream:

... An application uses a data output stream to write data that can later be read by a data input stream. ...

and that file nay seems to have been generated by a DataOutputStream, but the intention of DataInputStream is to read files in the format generated by a DataOutputStream. The method readUTF, for example, requires two bytes indicating the size of the text, followed by the text in the modified UTF-8 format. Similarly, if the file size is not fixed, it is advisable to include the number of elements in the file before writing the elements. This way when reading the file, it is not necessary to detect the end of the file.

Also, text files should normally be read by one of the classes *Reader (BufferedReader, FileReader, ...) to convert bytes to char. In the above case probably the best option is the BufferedReader because it offers the method readLine returning null to indicate the end of the archive.

Another option would be to use the Scanner, that I personally nay advise (without having studied the documentation thoroughly)

  • The file is all binary, the mirror I put up is just to try to explain how this data is inside the file

Browser other questions tagged

You are not signed in. Login or sign up in order to post.