![](/img/trans.png)
[英]How can I get a java.io.InputStream from a java.lang.String?
[英]Java:How can i get the encoding from inputStream?
我想从流中获取编码。
第一种方法-使用InputStreamReader。
但是它总是返回OS编码。
InputStreamReader reader = new InputStreamReader(new FileInputStream("aa.rar"));
System.out.println(reader.getEncoding());
输出:GBK
第二种方法-使用UniversalDetector。
但是它总是返回null。
FileInputStream input = new FileInputStream("aa.rar");
UniversalDetector detector = new UniversalDetector(null);
byte[] buf = new byte[4096];
int nread;
while ((nread = input.read(buf)) > 0 && !detector.isDone()) {
detector.handleData(buf, 0, nread);
}
// (3)
detector.dataEnd();
// (4)
String encoding = detector.getDetectedCharset();
if (encoding != null) {
System.out.println("Detected encoding = " + encoding);
} else {
System.out.println("No encoding detected.");
}
// (5)
detector.reset();
输出:空
我怎样才能正确? :(
让我们恢复情况:
因此,在阅读之前,需要了解编码。 您首先使用字符集检测类正确完成了所有操作。
阅读http://code.google.com/p/juniversalchardet/时,它应该处理UTF-8和UTF-16。 您可以使用编辑器JEdit来验证编码,并查看是否存在问题。
public String getDecoder(InputStream inputStream) {
String encoding = null;
try {
byte[] buf = new byte[4096];
UniversalDetector detector = new UniversalDetector(null);
int nread;
while ((nread = inputStream.read(buf)) > 0 && !detector.isDone()) {
detector.handleData(buf, 0, nread);
}
detector.dataEnd();
encoding = detector.getDetectedCharset();
detector.reset();
inputStream.close();
} catch (Exception e) {
}
return encoding;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.