一、mysql设置utf8mb4编码
目前mysql5.5以上的版本是可以支持utf8mb4编码的,我用的是mysql5.6。关于mysql设置存储utf8mb4,参考http://www.w2bc.com/Article/8533,也可以在navicat中选择某一个属性,直接设置,如下图。
二、过滤表情符
我最终的目的是把文本从mysql再导入到R语言中来进行文本处理,但目前R3.2.2版本貌似并不支持utf8mb4,只能使用utf8来读取文本,但是mysql里使用的是utf8mb4,对于含有表情符的文本,读入到R中就是NA,是不行的,所以考虑过滤掉这些表情符号。使用java过滤表情符,其实就是查找替换的过程,使用pattern,和matcher这两个类就可以解决。
java过滤表情符代码:
定义了Filter接口:
public interface Filter {
String filter(String str);
}
表情符过滤器类EmojiFilter:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmojiFilter implements Filter {
@Override
public String filter(String str) {
if(str.trim().isEmpty()){
return str;
}
String pattern="[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]";
String reStr="";
Pattern emoji=Pattern.compile(pattern);
Matcher emojiMatcher=emoji.matcher(str);
str=emojiMatcher.replaceAll(reStr);
return str;
}
}
逐行过滤文本main:
import java.io.*;
public class main {
public static void main(String[] args) {
String fin="F:/in.txt";
String fon="F:/out.txt";
EmojiFilter filter=new EmojiFilter();
try {
File filename = new File(fin);
InputStreamReader reader = new InputStreamReader(
new FileInputStream(filename));
BufferedReader br = new BufferedReader(reader);
File writename = new File(fon);
writename.createNewFile();
BufferedWriter out = new BufferedWriter(new FileWriter(writename));
String line = "";
line = br.readLine();
while(line!=null){
System.out.println(line);
line=filter.filter(line);
out.write(line+"\n");
out.flush();
line = br.readLine();
}
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
基础上可以过滤掉emoji表情符啦!!再导入mysql中,采用utf8编码,实测没有错误!
参考:http://blog.csdn.net/kobejayandy/article/details/44179925
emoji处理:http://blog.csdn.net/ugg/article/details/44225723