I’m getting some documents from the web and many of them are binary files (executables, PDF, etc.). In Java, what is the correct type to hold the binary data until save it in database? Can I use a String for this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use a byte array (byte[]) or InputStream (e.g. ByteArrayInputStream). Java Strings are not a good container for generic binary data.
The Apache library commons-io has some nice utility classes for dealing with bytes and streams.
e.g. IOUtils.toByteArray(InputStream)
ByteBuffer was introduced as part of Java NIO, available in Java 4 (1.4) and later. In specialized scenarios, it can have performance benefits over using a byte[]. It also has some helpful convenience methods. I still usually use byte[], though, since it is more widely known, more common in APIs, and almost always performs well enough.