0

I am trying to store a list of numbers(bytes) into a file so that I can retrieve them into a byte[].

59 20 60 21 61 22 62 23 63 24 64 25 65 26 66 27 67 28 68 29 
67 30 66 31 65 32 64 33 63 34 62 35 61 36 60 37 59 38
66 29 65 30 64 31 63 32 62 33 61 34 60 35 59 36 58 37
65 28 64 29 63 30 62 31 61 32 60 33 59 34 58 35 57 36...

I have tried saving them into a text file but the relevant code doesn't seem to read it properly.

    try {
        File f = new File("cube_mapping2.txt");
        array = new byte[file.size()]
        FileInputStream stream = new FileInputStream(f);
        stream.read(array);
    } catch (Exception e) {
        e.printStackTrace();
    }

Is there a proper way to save the file so that FileInputReader.read(byte[] buffer) will populate the array with my bytes?

4
  • What you're doing should work. What is the size of 'array'? What does stream.available() return? Commented Aug 1, 2011 at 2:29
  • The method of reading the byte depends on the method you use to write the bytes. Do you output the bytes as a text file and try to read it as if it is a binary file? I see no reason why your code should not work if you store and read the bytes as binary. Commented Aug 1, 2011 at 2:47
  • That's mainly my concern in the question. Is FileInputStream reading as a binary file? I tried saving the numbers in a text file and obviously that didn't work. Would using a hex editor to create a binary file suffice? Commented Aug 1, 2011 at 2:57
  • FileInputStream reads every file, including text file, as binary file. If you have a primitive array of bytes, you can write them using FileOutputStream.write(byte[]) without converting them and read them back using your code above. Commented Aug 1, 2011 at 3:53

3 Answers 3

3

I'd be using Scanner. Something like this:

public static void main(String[] args) throws IOException {
    InputStream stream = new FileInputStream("cube_mapping2.txt");
    Scanner s = new Scanner(stream);
    List<Byte> bytes = new ArrayList<Byte>();
    while (s.hasNextByte()) {
        bytes.add(s.nextByte());
    }
    System.out.println(bytes);
}

I tested this on a file containing your exact input and it worked. Output was:

[59, 20, 60, 21, 61, 22, 62, 23, 63, 24, 64, 25, 65, 26, 66, 27, 67, 28, 68, 29, 67, 30, 66, 31, 65, 32, 64, 33, 63, 34, 62, 35, 61, 36, 60, 37, 59, 38, 66, 29, 65, 30, 64, 31, 63, 32, 62, 33, 61, 34, 60, 35, 59, 36, 58, 37, 65, 28, 64, 29, 63, 30, 62, 31, 61, 32, 60, 33, 59, 34, 58, 35, 57, 36]
Sign up to request clarification or add additional context in comments.

7 Comments

I agree. You can then save each byte into a List<Byte>.
This doesn't really answer the question. I agree it is an overall better technique, but does not address the OP's concerns..
That does seem like a better way of reading, but as KyleM pointed out, does this properly read my numbers if they are simply stored in a text file?
Yes - it does work. I edited the question to include working code
I finally got around to testing it myself and it does in face work. Though I found it to be extremely slow as my actual file contains close to a thousand values.
|
2

FileInputStream works on binary files. The code you posted would read from a binary file, but isn't quite right because stream.read(array) reads up to the length of the array; it doesn't promise to read the whole array. The return value from read(array) is the number of bytes actually read. To be sure of getting all the data you want you need the read() call to be in a loop.

To answer your actual question: to write to a file in such a way that stream.read(array) will be able to read it back it, use FileOutputStream.write(array).

If you're happy with a text file instead of a binary file, go with @Bohemian's answer.

2 Comments

Thank you for the answer, I will test and see if this is a more efficient way than Bohemian's answer, though his does accomplish what I asked.
I ended up using both this and Bohemian's answer to solve my issue due to the constraints of developing on a mobile(Android), I need to use binary files to speed things up.
0
array = new byte[file.size()]

Is that means, there is no space left to store the separate mark for each two numbers? According to your byte array, if each one of them is only two spaces, then you can use a two spaces temp byte array to read each byte you store in your file. Something like

byte[] temp = new byte[2];
stream.read(temp);

Which can make sure to read the byte number one by one.

1 Comment

What? If he creates an array the size of the contents of the file... then populates the array from the file... then there will be enough space.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.