0

I'm looking for a way to switch between reading bytes (as byte[]) and reading lines of Strings from a file. I know that a byte[] can be obtained form a file through a FileInputStream, and a String can be obtained through a BufferedReader, but using both of them at the same time is proving problematic. I know how long the section of bytes are. String encoding can be kept constant from when I write the file. The filetype is a custom one that is still in development, so I can change how I write data to it.

How can I read Strings and byte[]s from the same file in java?

pensono
  • 216
  • 5
  • 17
  • Always read one type and convert when needed? You can convert String to byte[] and byte[] to String. – Simon Arsenault Jun 18 '13 at 22:17
  • 3
    Why not read the entire file as a byte array and then convert the needed sections to Strings after reading the file in? – AndyPerfect Jun 18 '13 at 22:18
  • Use [`DataInputStream`](http://docs.oracle.com/javase/6/docs/api/java/io/DataInputStream.html) and `DataOutputStream` to keep a consistent format? – Louis Wasserman Jun 18 '13 at 22:31

4 Answers4

1

Read as bytes. When you have read a sequence of bytes that you know should be a string, place those bytes in an array, put the array inside a ByteArrayInputStream and use that as the underlying InputStream for a Reader to get the bytes as characters, then read those characters to produce a String.

For the later parts of this process see the related SO question on how to create a String from an InputStream.

Community
  • 1
  • 1
Raedwald
  • 40,290
  • 35
  • 127
  • 207
0

Read the file as Strings using a BufferedReader then use String.getBytes().

Ravi K Thapliyal
  • 46,997
  • 8
  • 70
  • 84
0

Why not try this:

        BufferedReader bufferedReader = null;

        try {
            bufferedReader = new BufferedReader(new FileReader("testing.txt"));
            String line = bufferedReader.readLine();
            while(line != null){
                byte[] b = line.getBytes();
            }

        } finally {
            if(bufferedReader!=null){
                bufferedReader.close();
            }
        }

or

            FileInputStream in = null;
        BufferedReader bufferedReader = null;

        try {
            bufferedReader = new BufferedReader(new FileReader("xanadu.txt"));
            String line = bufferedReader.readLine();
            while(line != null){
                //read your line
            }
            in = new FileInputStream("xanadu.txt");
            int c;
            while ((c = in.read()) != -1) {
                //read your bytes (c)
            }


        } finally {
            if (in != null) {
                in.close();
            }
            if(bufferedReader!=null){
                bufferedReader.close();
            }
        }
Indu Devanath
  • 1,682
  • 13
  • 17
0

Read everything as bytes from the buffered input stream, and convert string sections into String's using constructor that accepts the byte array:

 String string = new String(bytes, offset, length, "US-ASCII"); 

Depending on how the data are actually encoded, you may need to use "UTF-8" or something else as the name of the charset.

Audrius Meskauskas
  • 18,378
  • 9
  • 63
  • 78