将二进制文件读入字符串

发布于 2024-12-05 06:57:44 字数 532 浏览 0 评论 0原文

这一定是显而易见的,但我无法弄清楚。 为此我花了几乎一整天的时间。 我很乐意给能让我放松的人买瓶啤酒。

File file = new File(filePath);
byte[] bytes = new byte[(int)file.length()];
DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));
dataInputStream.readFully(bytes);           
dataInputStream.close();            
return new String(bytes);

这是我的代码。 我发现字节数组大小不合适,但我无法找出正确的大小。 除此之外,内容也不正确。 看来只有文字字符才可以。

看来从二进制文件中取出数据真是一件痛苦的事,我真的很郁闷。

还有一件事:文件内容不是文本,它可以是图片、视频或 pdf 等任何内容。

It must be obvious, but I can't figure it out.
I spent almost a whole day for this.
I'll gladly buy a beer to someone who can lighten me.

File file = new File(filePath);
byte[] bytes = new byte[(int)file.length()];
DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));
dataInputStream.readFully(bytes);           
dataInputStream.close();            
return new String(bytes);

This is my code.
I see that the byte array size is not OK, but I can't figure out the right size.
Besides that the contents are not also incorrect.
It seems that only the text characters are OK.

It seems get out the data from a binary file is a real pain, I'm really depressed.

One more thing: the file contents are not text, it can be anything like a picture, video, or pdf.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有你我更好 2024-12-12 06:57:44

如果您正在阅读二进制文件,则应该不是 尝试将其视为编码文本。将其转换为这样的字符串是不合适的 - 您应该将其保留为字节数组。如果您确实需要它作为文本,则应使用base64或十六进制来表示二进制数据 - 其他方法可能会丢失数据。

如果良好的没有例外返回,则表明它的读取的数据是您要求的,这应该是整个文件。您已经设法很容易地从二进制文件中获取数据(尽管collect()呼叫应该在最后一个块中) - 它只是将其转换为文本,这是一个坏主意。

If you're reading a binary file you should not try to treat it as if it were encoded text. It's inappropriate to convert it to a string like this - you should keep it as a byte array. If you really need it as text, you should use base64 or hex to represent the binary data - other approaches are likely to lose data.

If readFully returned without an exception, that shows it's read as much data as you requested, which should be the whole file. You've managed to get the data from a binary file fairly easily (although the close() call should be in a finally block) - it's only converting it to text which is a bad idea.

纸短情长 2024-12-12 06:57:44

正如乔恩·斯基特(Jon Skeet)告诉您的那样(而且您应该始终听一个具有347k!的人!),如果不是文本,请不要将其保存在字符串中,并将其保存为字节阵列。另外,尝试CONSONS-IO并使用其辅助类别。

File file = new File(filePath);
InputStream is  = null;
byte[] bytes = null;
try{
    bytes = IOUtils.toByteArray(is);
}finally{
    IOUtils.closeQuietly(is)
}

As Jon Skeet told you (and you should always listen someone with 347k!), if it is not a text, do not save it in a string and keep it as a byte array. Also, try commons-io and use its helper classes.

File file = new File(filePath);
InputStream is  = null;
byte[] bytes = null;
try{
    bytes = IOUtils.toByteArray(is);
}finally{
    IOUtils.closeQuietly(is)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文