2

Earlier I was storing only string in my file which can be store in SD card now I would to store byte[] also in same files. So do I just need to store normal to the file like this:

for string:- bufferWritter.write(data);
for bytes:- FileUtils.writeByteArrayToFile(new File("pathname"), myByteArray) or fos.write(myByteArray);

So if I do like that then how can I differ message whether it is string or byte[].

Even I would like to know that is this is a good way to do?

3
  • It is not very clean, in my opinion. Could you clarify your purpose ? Commented Jun 19, 2016 at 19:12
  • I need to store messages which can be a normal string or a image(which is converted into byte array) in a same file. Commented Jun 19, 2016 at 19:14
  • @TinTran And one more thing i need to send this file to server(If i am not sending the images to the server so i may only store the image path in my file and store image in my local storage). Commented Jun 19, 2016 at 19:16

2 Answers 2

1

You would need to write some sort of header to your file which should state what it does represent. If you have multiple items in one file, also specify the length of the data parts.

[byte] sort of data (0=String, 1=Image)

And then the actual data.

But I would recommend you use a different format like json or make a serializable object.

Sign up to request clarification or add additional context in comments.

Comments

1

I would give a try to some JSON implementation (maybe GSON? or some alternative) so you can have stored mixed data types in one file or write your own de/serialization routine so you can store whole objects.

Note: if you implement Serializable interface by a class that represent object to be stored, don't forget to re-generate UUID each time you change contents of that class, it will save you some time figuring out what went wrong

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.