你可以把它包起来
读卡器
使用简单类解码代理项对的实例:
import java.io.Closeable;
import java.io.IOException;
import java.io.Reader;
public class CodepointStream implements Closeable {
private Reader reader;
public CodepointStream(Reader reader) {
this.reader = reader;
}
public int read() throws IOException {
int unit0 = reader.read();
if (unit0 < 0)
return unit0; // EOF
if (!Character.isHighSurrogate((char)unit0))
return unit0;
int unit1 = reader.read();
if (unit1 < 0)
return unit1; // EOF
if (!Character.isLowSurrogate((char)unit1))
throw new RuntimeException("Invalid surrogate pair");
return Character.toCodePoint((char)unit0, (char)unit1);
}
public void close() throws IOException {
reader.close();
reader = null;
}
}
主要的
功能需要稍加修改:
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
public final class App {
public static void main(String args[]) {
CodepointStream reader = new CodepointStream(
new InputStreamReader(System.in, StandardCharsets.UTF_8));
try {
System.out.print("> ");
int code = reader.read();
while (code != -1) {
String s =
String.format("Code %x is `%s', %s.",
code,
Character.getName(code),
new String(Character.toChars(code)));
System.out.println(s);
code = reader.read();
}
} catch (Exception e) {
}
}
}
> keyboard â¨. pizza ð
Code 6b is `LATIN SMALL LETTER K', k.
Code 65 is `LATIN SMALL LETTER E', e.
Code 79 is `LATIN SMALL LETTER Y', y.
Code 62 is `LATIN SMALL LETTER B', b.
Code 6f is `LATIN SMALL LETTER O', o.
Code 61 is `LATIN SMALL LETTER A', a.
Code 72 is `LATIN SMALL LETTER R', r.
Code 64 is `LATIN SMALL LETTER D', d.
Code 20 is `SPACE', .
Code 2328 is `KEYBOARD', â¨.
Code 2e is `FULL STOP', ..
Code 20 is `SPACE', .
Code 70 is `LATIN SMALL LETTER P', p.
Code 69 is `LATIN SMALL LETTER I', i.
Code 7a is `LATIN SMALL LETTER Z', z.
Code 7a is `LATIN SMALL LETTER Z', z.
Code 61 is `LATIN SMALL LETTER A', a.
Code 20 is `SPACE', .
Code 1f355 is `SLICE OF PIZZA', ð.
Code a is `LINE FEED (LF)',
.