Reading Strings and Binary from the same FileInputStream

前端 未结 7 1974
离开以前
离开以前 2021-01-17 22:29

I have a file that contains some amount of plain text at the start followed by binary content at the end. The size of the binary content is determined by some one of the pla

相关标签:
7条回答
  • 2021-01-17 22:41

    Alas, DataInputStream is deprecated and does not handle UTF. But this should help (it reads a line from a binary stream, without any lookahead).

    public static String lineFrom(InputStream in) throws IOException {
        byte[] buf = new byte[128];
        int pos = 0;
        for (;;) {
            int ch = in.read();
            if (ch == '\n' || ch < 0) break;
            buf[pos++] = (byte) ch;
            if (pos == buf.length) buf = Arrays.copyOf(buf, pos + 128);
        }
        return new String(Arrays.copyOf(buf, pos), "UTF-8");
    }
    
    0 讨论(0)
  • 2021-01-17 22:48

    If you genuinely have a file (rather than something harder to seek in, e.g. a network stream) then I suggest something like this:

    • Open the file as a FileInputStream
    • Wrap it in InputStreamReader and a BufferedReader
    • Read the text, so you can find out how much content there is
    • Close the BufferedReader (which will close the InputStreamReader which will close the FileInputStream)
    • Reopen the file
    • Skip to (total file length - binary content length)
    • Read the rest of the data as normal

    You could just call mark() at the start of the FileInputStream and then reset() and skip() to get to the right place if you want to avoid reopening the file. (I was looking for an InputStream.seek() but I can't see one - I can't remember wanting it before in Java, but does it really not have one? Ick.)

    0 讨论(0)
  • 2021-01-17 22:48

    I recommend using DataInputStream. You have the following options:

    • Read both text and binary content with DataInputStream
    • Open a BufferedReader, read text and close the stream. Then open a DataInputStream, skip bytes equal to the size of the text and read binary data.
    0 讨论(0)
  • 2021-01-17 22:52

    You could use RandomAccessFile. Use readLine() to read the plain text at the start (note the limitations of this, as described in the API), and then readByte() or readFully() to read the subsequent binary data.

    Using the underlying FileInputStream to read returns empty byte arrays.

    That's because you have wrapped the stream in a BufferedReader, which has probably consumed all the bytes from the stream when filling up its buffer.

    0 讨论(0)
  • 2021-01-17 23:01

    You need to use an InputStream. Readers are for character data. Look into wrapping your input stream with a DataInputStream, like:

    stream=new DataInputStream(new BufferedInputStream(new FileInputStream(...)));
    

    The data input stream will give you many useful methods to read various types of data, and of course, the base InputStream methods for reading bytes.

    (This is actually exactly what a HTTP server must do to read a request with content.)


    The readUTF doesn't read a line, it reads a string that was written in (modified) UTF8 format - refer to the JavaDoc.

    0 讨论(0)
  • 2021-01-17 23:04

    The correct way is to use an InputStream of some form, probably a FileInputStream unless this becomes a performance barrier.

    What do you mean "Using the underlying FileInputStream to read returns empty byte arrays."? This seems very unlikely and is probably where your mistake is. Can you show us the example code you've tried?

    0 讨论(0)
提交回复
热议问题