什么类型是最好用Java来管理二进制数据? [英] What type is the best to manage binary data in Java?

查看:189
本文介绍了什么类型是最好用Java来管理二进制数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到从网上的一些文件,其中许多人是二进制文件(可执行文件,PDF等)。在Java中,什么是正确的类型来保存二进制数据,直到将其保存在数据库中?我是否可以使用String此?

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?

推荐答案

使用一个字节数组(字节[])或InputStream的(例如ByteArrayInputStream的)。 Java的字符串是不通用的二进制数据的良好的容器中。

Use a byte array (byte[]) or InputStream (e.g. ByteArrayInputStream). Java Strings are not a good container for generic binary data.

阿帕奇库公共-IO 有一些不错的实用工具类来处理字节和溪流。

The Apache library commons-io has some nice utility classes for dealing with bytes and streams.

例如。 <一href=\"http://commons.apache.org/io/api-release/org/apache/commons/io/IOUtils.html#toByteArray%28java.io.InputStream%29\">IOUtils.toByteArray(InputStream)

的ByteBuffer 被介绍为的的Java NIO ,在Java中4(1.4)及更高版本。在专门情况下,它可以具有比使用一个字节[]的性能优势。它也有一些有用的方便的方法。我还是平时的byte []使用,不过,因为它更广为人知,在API的更常见,而且几乎总是表现不够好。

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.

这篇关于什么类型是最好用Java来管理二进制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆