在我读取文件之前,如何确定文件使用哪个编码? [英] How can I determine which encoding the file uses before I read the file?

查看:120
本文介绍了在我读取文件之前,如何确定文件使用哪个编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题。

一个文件可以用一些编码,如 UTF-8 UTF-16 UTF-32 等。

A file can be written in some encoding such as UTF-8, UTF-16, UTF-32, etc.

当我读取 UTF-16 文件时,我使用以下代码:

When I read a UTF-16 file, I use the code below:

 BufferedReader in = new BufferedReader(
                           new InputStreamReader(
                           new FileInputStream(file), "UTF16"));

如何在读取文件之前确定文件的编码?

How can I determine which encoding the file is in before I read the file ?

当我使用 UTF-16 读取 UTF-8

推荐答案

没有好的方法可以做到这一点。你问的问题就像通过查看数字来确定数字的基数。例如, 101

There is no good way to do that. The question you're asking is like determining the radix of a number by looking at it. For example, what is the radix of 101?

的基数是最好的解决方案是将数据读入字节数组。然后,您可以使用字符串(字节[]字节,字符集字符集)以使用多个编码进行测试,很可能最不可能。

Best solution would be to read the data into a byte array. Then you can use String(byte[] bytes, Charset charset) to test it with multiple encodings, most likely to least likely.

这篇关于在我读取文件之前,如何确定文件使用哪个编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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