Java, need a while loop to reach eof. i.e.while !eof, keep parsing

前端 未结 3 565
花落未央
花落未央 2021-01-14 10:45

I currently have a working parser. It parses a file once(not what I want it to do) and then outputs parsed data into a file. I need it to keep parsing and appending to the s

3条回答
  •  鱼传尺愫
    2021-01-14 11:34

    DataInputStream has a lot of readXXX() methods that do throw EOFException but the method that you're using DataInputStream.read() does not throw EOFException.

    To correctly identify the EOF while using read() implement your while loop as follows

    int read = 0;
    byte[] b = new byte[1024];
    while ((read = dis.read(b)) != -1) { // returns numOfBytesRead or -1 at EOF
      // parse, or write to output stream as
      dos.write(b, 0, read); // (byte[], offset, numOfBytesToWrite)
    }
    

提交回复
热议问题