Identify the EOF in a DataInputStream

1

I'm doing a program that needs to read a binary file and go extracting some information from it, what I have so far is this:

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 of 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 concerns while, is there any way to check when I get to the end of the objectIn? Currently I just have while (true) and I expect the program to fire a Exception . But that seems wrong to me anyway.

    
asked by anonymous 19.08.2017 / 02:01

1 answer

1
  

That's how I managed to do it, but do you agree that this seems "wrong"?

Yes, seems a bit wrong. At least documentation of DataInputStream :

  

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

and this not file appears to have been generated by a DataOutputStream , but the DataInputStream intention is to read files in the format generated by a DataOutputStream . The readUTF method, 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. That way at the time of reading the file, it is not necessary to detect the end of the file.

In addition, text files should normally be read by one of the *Reader ( BufferedReader , FileReader , ...) classes to do bytes conversion to char . In the above case the best option is BufferedReader because it offers the readLine method that returns null to indicate the end of the file.

Another option would be to use Scanner , which I personally do not recommend (without having thoroughly studied the documentation)

    
19.08.2017 / 08:59