在读取双字节字符时,主要涉及到编码的选取:
- public static boolean isRightfulTXT(File f) {
- // TODO Auto-generated method stub
- String regexp="[^\x00-\xff]";//双字节字符
- Pattern p=Pattern.compile(regexp);
- try {
- FileInputStream fis=new FileInputStream(f);
- //"GBK"编码方式支持双字节字符
- InputStreamReader isr=new InputStreamReader(fis, "GBK");
- BufferedReader br=new BufferedReader(isr);
- String line="";
- while((line=br.readLine())!=null){
- //逐行读取文件,
- //检索文件中是否含有双字节字符
- Matcher m=p.matcher(line);
- if(m.find()){
- fis.close();
- isr.close();
- br.close();
- return false;
- }
- }
- fis.close();
- isr.close();
- br.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return true;
- }
以上代码功能:检测txt文件中是否含有双字节字符,若有返回假,否则返回真。