你如何使用StringTokenizer从.text中提取一个单词?

时间:2014-02-03 16:27:27

标签: java

我一直试图让我的程序从我的.txt文件中拉出一个单词,但我必须使用一个标记器来完成它,但我似乎无法正确使用它。

public void getWord(){
    ASCIIDataFile in=new ASCIIDataFile("JavaReservedWords.txt");    
    while (in.isEOF()) {
        String word = in.readString();
        StringTokenizer st = new StringTokenizer(word);

        while(st.hasMoreTokens()){
        System.out.println(st.nextToken());
    }
    in.close();
}

我不知道你限制它。

4 个答案:

答案 0 :(得分:1)

文本文件中使用的分隔符是什么?另外,你应该改变

while(in.isEOF())

while(!in.isEOF())

答案 1 :(得分:0)

你想打印一个单词吗?无法理解你的期望。 如果您想将输出限制为每行的第一个单词,那么您应该

if (st.hasMoreTokens())
    System.out.println(st.nextToken());

答案 2 :(得分:0)

这是您指定的。它打开一个文件并遍历每一行中的每个单词。

import  java.io.File;
import  java.io.IOException;
import  java.util.regex.Matcher;
import  java.util.regex.Pattern;
import  org.apache.commons.io.FileUtils;
import  org.apache.commons.io.LineIterator;

/**
   <P>{@code java FileWordIteratorXmpl regex_to_word_iterate.txt}</P>
 **/
public class FileWordIteratorXmpl  {
   public static final void main(String[] as_1RqdTxtFilePath)  {
      LineIterator li = null;
      try  {
         li = FileUtils.lineIterator(new File(as_1RqdTxtFilePath[0])); //Throws npx if null
      }  catch(IOException iox)  {
         throw  new RuntimeException("Attempting to open \"" + as_1RqdTxtFilePath[0] + "\"", iox);
      }  catch(RuntimeException rtx)  {
         throw  new RuntimeException("One required parameter: The path to the text file.", rtx);
      }

      //Dummy search string (""), so it can be reused (reset)
      Matcher mWord = Pattern.compile("\\b\\w+\\b").matcher("");

      while(li.hasNext())  {
         String sLine = li.next();
         mWord.reset(sLine);

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

   }
}

使用此文件:

Hello there
Hello1 there1
Hello2 there2
Hello3 there3
Hello4 there4
Hello5 there5

输出:

[C:\java_code\]java FileWordIteratorXmpl C:\java_code\text_to_word_iterate.txt
Hello
there
Hello1
there1
Hello2
there2
Hello3
there3
Hello4
there4
Hello5
there5

答案 3 :(得分:0)

  

我无法访问ASCIIDataFile,因此我使用了一个   的BufferedReader:

import java.io.BufferedReader;

import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class Main {

    StringBuilder sb = new StringBuilder();

    StringTokenizer tokenizer;

    public static void main(String[] args){
        Main main = new Main();
        System.out.println(main.getWord());
        System.out.println(main.getWord());
        System.out.println(main.getWord());
    }

    public Main(){
        BufferedReader reader; // replace with your ASCIIDataFile

        try {

            // additionally update this with your ASCIIDataFile as needed
            reader = new BufferedReader(new FileReader("your-file-here"));
            String read;

            while((read = reader.readLine()) != null) {
                sb.append(read);
                sb.append(","); // or whatever delimiter you want
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        tokenizer = create();
    }

    public String getWord(){

        if (tokenizer.hasMoreTokens()) {
            return tokenizer.nextToken();
        }
        else{
            return null; // or throw an exception
        }
    }

    public void reset(){
        tokenizer = create();  // allows to interate through tokens again
    }

    private StringTokenizer create(){
        return new StringTokenizer(sb.toString(), ",");
    }
}