txt阅读器代码java_Java:阅读器和编码

小编典典

读者如何知道他必须使用UTF-8?

通常你指定 你自己

的一个InputStreamReader。它有一个采用字符编码的构造函数。例如

Reader reader = new InputStreamReader(new FileInputStream("c:/foo.txt"), "UTF-8");

所有其他读者(据我所知)都使用平台默认字符编码,这实际上可能不是正确的编码(例如 -cough- CP-1252)。

从理论上讲,您还可以根据字节顺序标记自动检测字符编码。这将几种unicode编码与其他编码区分开来。不幸的是Java

SE对此没有任何API,但是您可以自制一个可以用来替换的API,InputStreamReader如上面的示例所示:

public class UnicodeReader extends Reader {

private static final int BOM_SIZE = 4;

private final InputStreamReader reader;

/**

* Construct UnicodeReader

* @param in Input stream.

* @param defaultEncoding Default encoding to be used if BOM is not found,

* or null to use system default encoding.

* @throws IOException If an I/O error occurs.

*/

public UnicodeReader(InputStream in, String defaultEncoding) throws IOException {

byte bom[] = new byte[BOM_SIZE];

String encoding;

int unread;

PushbackInputStream pushbackStream = new PushbackInputStream(in, BOM_SIZE);

int n = pushbackStream.read(bom, 0, bom.length);

// Read ahead four bytes and check for BOM marks.

if ((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) && (bom[2] == (byte) 0xBF)) {

encoding = "UTF-8";

unread = n - 3;

} else if ((bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) {

encoding = "UTF-16BE";

unread = n - 2;

} else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) {

encoding = "UTF-16LE";

unread = n - 2;

} else if ((bom[0] == (byte) 0x00) && (bom[1] == (byte) 0x00) && (bom[2] == (byte) 0xFE) && (bom[3] == (byte) 0xFF)) {

encoding = "UTF-32BE";

unread = n - 4;

} else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE) && (bom[2] == (byte) 0x00) && (bom[3] == (byte) 0x00)) {

encoding = "UTF-32LE";

unread = n - 4;

} else {

encoding = defaultEncoding;

unread = n;

}

// Unread bytes if necessary and skip BOM marks.

if (unread > 0) {

pushbackStream.unread(bom, (n - unread), unread);

} else if (unread < -1) {

pushbackStream.unread(bom, 0, 0);

}

// Use given encoding.

if (encoding == null) {

reader = new InputStreamReader(pushbackStream);

} else {

reader = new InputStreamReader(pushbackStream, encoding);

}

}

public String getEncoding() {

return reader.getEncoding();

}

public int read(char[] cbuf, int off, int len) throws IOException {

return reader.read(cbuf, off, len);

}

public void close() throws IOException {

reader.close();

}

}

编辑 作为对您的编辑的答复:

因此编码取决于操作系统。 因此,这意味着并非在每个操作系统上都是如此:

'a'== 97

不,这不是真的。的ASCII(其含有128个字符,编码0x00,直到与0x7F)为

基础

的所有其它的字符编码。只有字符ASCII集以外的字符可能会冒用其他编码显示不同的风险。该ISO-8859编码涵盖了人物ASCII以相同的代码点范围。该Unicode编码涵盖了人物ISO-8859-1以相同的代码点范围。

您可能会发现每个博客都很有趣:

2020-09-24

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值