有一个txt文件,里面有英文单词,标点符号,
如何逐词读取?去除各种符号,把单词读取出来

解决方案 »

  1.   

    根据ASCII吗应该可以比较的吧 
      

  2.   

    使用 String replaceAll(String regex, String replacement) 将所有的非单词字符全部替换成一个统一的非单词字符 然后再用stringtokenizer进行解析 应该可以吧 我没有试过啊 呵呵 仅仅提供建议 
      

  3.   

    用正则表达式匹配吧.
    import java.util.*;
    import java.io.*;public class Test
    {
    public static void main(String[] args)
    {
    String s = "\\W+";//匹配单词
    Scanner sc = null;
    try
    {
    sc = new Scanner(new File("1.txt")).useDelimiter(s);
    }catch(Exception e)
    {
    System.out.println("can not find the file");
    System.exit(1);
    }
    while(sc.hasNext())
    System.out.print(sc.next() + " ");
    }
    }
      

  4.   

    先把整个txt文件input,然后转化成一个str.
    接着用str.split("\\W+"")把该字符串转换成字符串数组strArray[]
    最后for循环输出单词
      

  5.   

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class Test02 {
    public static void main(String[] args) {

    String exp = "\\w+";
    File f = new File("d:\\hehe.txt");
    String str = null;
    String content = "";
    Pattern p = Pattern.compile(exp);
    Matcher m = null;

    try {
    BufferedReader br = new BufferedReader(new FileReader(f));
    while ((str = br.readLine()) != null) {
    content += str;
    }
    m = p.matcher(content);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }

    while (m.find()) {
    System.out.println(m.group());
    }
    }
    }
      

  6.   

    接11楼:供参考public class TestString1 {
        public static void main(String[] args){
         String str="The good student is a beautiful girl!And she,&&,has two dogs.";
        
         String[] strArray=str.split("\\W+");
         for(int i=0;i<strArray.length;i++){
         System.out.print("  "+strArray[i]);
         }
        }
    }
    运行结果:
      The  good  student  is  a  beautiful  girl  And  she  has  two  dogs