日期:2014-05-20  浏览次数:20759 次

java中如何在中文情况下使用split方法
比方说一行字符串以|分割,全是中文 怎么用split方法拆分
eg:你好嘛|很好|不好
怎么分割成一个String 类型的数组呢
或者
怎么使用中文搜索呢
比方说在 你好嘛|很好|不好 中找到很好
怎么使用中文搜索呢
谢谢

------解决方案--------------------
Java code

    public static String getFileContent(String path) throws IOException {
        StringBuffer sb = new StringBuffer();
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(path));
            String line = null;
            while ((line = in.readLine()) != null) {
                sb.append(line);
                sb.append("\n");
            }
        } finally {
            if (in != null) {
                in.close();
                in = null;
            }
        }
        return sb.toString();
    }

------解决方案--------------------
Java code

public String getEncoding(String path){
        //ANSI 0 0 0   utf8 -17 -69 -65  UNICODE -1 -2 0
        String code="";
        code="gb2312";
        try {
            InputStream inputStream=new FileInputStream(new File(path));
            byte []head=new byte[3];
            inputStream.read(head);
            if(head[0]==-1&&head[1]==-2){
                code="UTF-16";
            }
            if(head[0]==-2&&head[1]==-1){
                code="Unicode";
            }
            if((head[0]==-17&&head[1]==-69) || (head[0]==-26&&head[1]==-95)){
                code="UTF-8";
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        System.out.println(code);
        return code;
    }