How do I convert a java.io.File
to a byte[]
?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
It depends on what best means for you. Productivity wise, don't reinvent the wheel and use Apache Commons. Which is here
IOUtils.toByteArray(InputStream input)
.Let me add another solution without using third-party libraries. It re-uses an exception handling pattern that was proposed by Scott (link). And I moved the ugly part into a separate message (I would hide in some FileUtils class ;) )
Another Way for reading bytes from file
From JDK 7 you can use
Files.readAllBytes(Path)
.Example:
Simplest Way for reading bytes from file