Nar*_*put 0 java bash multibyte
我有一个包含多字节字符的文件.由于文件非常大,我想从这个文件中删除4字节的utf字符.我需要一个更快的解决方案来实现它.我为此任务尝试了以下java代码,但它为Java堆空间提供了内存不足异常.所以
import java.util.*;
import java.io.*;
public class A{
public static void main(String args[]) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
char[] c_array;
String c_string;
byte[] c_byte_array;
c_array = str.toCharArray();
for (char c : c_array){
c_string = Character.toString(c);
c_byte_array = c_string.getBytes("UTF-8");
if (c_byte_array.length <= 3){
System.out.print(c_string);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法改善这个或其他更快更好的方法来做到这一点.我也试过这个grep命令
grep -o -P "[\x10000-\x10ffff]" myfile
Run Code Online (Sandbox Code Playgroud)
但它退出时出错
grep: range out of order in character class
Run Code Online (Sandbox Code Playgroud)
所以我的问题是上面的正则表达式有什么问题.或者换句话说什么是匹配4字节utf字符的正则表达式.另一个问题是上述哪种方法可以更有效地删除4字节字符
Evg*_*eev 11
UTF-8 4字节字符是U + FFFF之上的Unicode字符(参见http://en.wikipedia.org/wiki/UTF-8),它们用Java表示为2个字符.我们可以使用Character.isSurrogate(请参阅API)来检测此类字符.
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("1.txt"), "UTF-8"));
for (int c; (c = br.read()) != -1;) {
if (Character.isSurrogate((char)c)) {
// skip the second surrogate char
br.read();
} else {
// process char c
}
}
Run Code Online (Sandbox Code Playgroud)
或者如果你想过滤字节流,那么我们可以使用另一种解决方案,基于4字节字符UTF-8序列是
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
所以我们可以将字节流过滤为
BufferedInputStream is = new BufferedInputStream(new FileInputStream("1.txt"));
for (int b; (b = is.read()) != -1;) {
if ((b & 0b11111000) == 0b11110000) {
// skip next 3 bytes
is.read();
is.read();
is.read();
} else {
// process byte b
}
}
Run Code Online (Sandbox Code Playgroud)