有一篇文本,文本中有中文跟英文,词与词之间用空格符分开,如:“支持  USB  充电  插上  usb线  电池格  变成  二格  没  确认”。文本已读到java.io.Reader中了,如何从该Reader中把每个词读出来?
我写的一个程序效果很不好:    private Reader input;  //输入文本
    private String currentToken;  //读取的一个词语
    private void readNextToken() {        StringBuffer buf = new StringBuffer();
        boolean endReached = false;  //判断是否达到文本结尾
        int in = 0;        try {            // Read from the stream, until a letter occurs            in = input.read();
            char ch = (char) in;            while ((in != -1) && (Character.isWhitespace(ch))){
                in = input.read();
                ch = (char) in;
            }            if (in != -1)
                buf.append(ch);            // Read from the stream, util a non-letter occurs            while ((in != -1) && !(Character.isWhitespace(ch))) {                in = input.read();
                ch = (char) in;                if (!Character.isWhitespace(ch))
                    buf.append(ch);            }
        } catch (Exception e) {
            endReached = true;        }        if (in == -1)
            endReached = true;        if (endReached) {            // If the stream ended with a non-empty token, this is the last
            // token, otherwise there is no more token.            if (buf.length() > 0)
                currentToken = buf.toString();
            else
                currentToken = null;            return;
        } else {            // if the end of the stream has not been reached yet, simply store
            // the extracted token.
            currentToken = buf.toString();
            return;
        }    }
请大侠帮忙!!大侠救救我吧!!

解决方案 »

  1.   

    你为何不都读取出来,然后用
    str.split(" "); 
    用空格分开就行了!
      

  2.   

    Reader只能一个字符一个字符读出来吧?
      

  3.   

    用BufferedReader的readline方法每次读入一行,然后split(" ");
      

  4.   

    或者StringTokenizer(" ")也行
      

  5.   

    读入的文本已经分好词了的,文本读到了Reader input中。
    in = input.read();
    char ch = (char) in;//读取一个字符,转为char型。是不是英文字符跟中文字符字节数不一样啊?