1

I am not able to find a clear answer to this. Does the ECLIPSE IDE support emojis? I have read a lot about surrogate pairs here on stack overflow, but I am unable to get a clear answer on this.

I am having to read in a text file character by character and I am using FileInputStream.

Would it be possible to process the emojis using surrogate pairs? I am wanting to use a select few apple emojis. These specifically: By process them, I mean I would like to identify them as that particular emoji when reading in the file.

If so, could someone show me an example?

4

1 回答 1

1

InputStreams 用于读取字节; 阅读器用于阅读字符。所以你应该使用从 Files.newBufferedReader 获得的 Reader,或者使用 FileReader 或 InputStreamReader。

尽管 Java 在 String 中使用代理对来表示表情符号和许多其他类型的 Unicode 字符,但您不需要直接处理代理对。char仅存在代理值是因为许多字符值对于 Java类型来说太大了。如果您将单个字符读取为int值(例如,使用CharSequence.codePoints方法),您每次都会获得整个字符值,并且您永远不会看到或必须处理代理值。

在撰写本文时,Unicode 将表情符号定义为表情符号块、补充符号和象形文字块的一部分以及杂项符号块中的三个传统字符。

因此,使用 BufferedReader 并使用整数遍历字符数据可能如下所示:

try (BufferedReader reader =
    Files.newBufferedReader(Paths.get(filename), Charset.defaultCharset())) {

    IntStream chars = reader.lines().flatMapToInt(String::codePoints);
    chars.forEachOrdered(c -> {
        if ((c >= 0x2639 && c <= 0x263b) ||
            (c >= 0x1f600 && c < 0x1f650) ||
            (c >= 0x1f910 && c < 0x1f930)) {

            processEmoji(c);
        }
    });
}
于 2016-11-01T20:36:57.603 回答