Reading strings and binary files from a single file FileInputStream

I have a file that contains some plain text at the beginning, followed by binary content at the end. The size of the binary content is determined by some one of the lines that I read.

I used BufferedReader to read single lines, however it does not provide methods for reading byte array references. readUTF for a DataInputStream does not read all the way to the end of the line, and the readLine method is deprecated.

Using the underlying FileInputStream for reading returns empty byte arrays. Any suggestions on how to do this?


 private DOTDataInfo parseFile(InputStream stream) throws IOException{ DOTDataInfo info = new DOTDataInfo(); BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); int binSize = 0; String line; while((line = reader.readLine()) != null){ if(line.length() == 0) break; DOTProperty prop = parseProperty(line); info.getProperties().add(prop); if(prop.getName().equals("ContentSize")) binSize = Integer.parseInt(prop.getValue()); } byte[] content = new byte[binSize]; stream.read(content); //Its all empty now. If I use a DataInputStream instead, its got the values from the file return info; } 
+7
java io stream
source share
7 answers

If you really have a file (and not something harder to look for, such as a network stream), I suggest something like this:

  • Open the file as FileInputStream
  • Wrap it in InputStreamReader and BufferedReader
  • Read the text to find out how much content is there.
  • Close BufferedReader (which closes InputStreamReader, which closes FileInputStream)
  • Reopen file
  • Go to (total file length - binary content length)
  • Read the rest of the data as usual.

You can simply call mark() at the beginning of FileInputStream, and then reset() and skip() to get to the right place if you want to avoid opening the file again. (I was looking for InputStream.seek() , but I can’t see it - I don’t remember it before in Java, but it really doesn’t have this? Ick.)

+3
source share

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

Using the underlying FileInputStream for reading returns empty byte arrays.

This is because you wrapped the stream in a BufferedReader , which probably absorbed all the bytes from the stream when the buffer was full.

+4
source share

You need to use InputStream. Readers are for character data. Look at transferring a data stream using a DataInputStream, for example:

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

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

(This is really what an HTTP server needs to do to read a request with content.)


readUTF does not read a line, it reads a line that was written in the (modified) UTF8 format - refer to JavaDoc.

+2
source share

Alas, DataInputStream deprecated and does not process UTF. But this should help (it reads a string from a binary stream without any look).

 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"); } 
+1
source share

The right way is to use some form of InputStream, perhaps FileInputStream, if that doesn't become a performance barrier.

What does it mean "Using a basic FileInputStream to read returns empty byte arrays"? This seems very unlikely and probably where your mistake is. Can you show us an example of the code you tried?

0
source share

You can read the text using BufferedReader. When you know where the binary is running, you can close the file and open it with RandomAccessFile and read the binary from anywhere in the file. Or you can read the file as binary and convert sections to the text that you identify as text. {Using a new line (bytes, encoding)}

0
source share

I recommend using DataInputStream . You have the following options:

  • Read both text and binary content with DataInputStream
  • Open BufferedReader, read the text and close the stream. Then open the DataInputStream, skip the bytes equal to the size of the text and read the binary data.
0
source share

All Articles