1

我目前正在逐个读取文件令牌,我想先读取整行,然后将其拆分为令牌。这是我通过令牌阅读的代码的一部分:

public void getNextToken(){
     String temp = input.next();        

     if(temp.equals("$")){
         nextToken = Symbols.ENDOFFILE;
     }
     else if(temp.equals("a")){
         nextToken = Symbols.IDENT;
     }
     else if(temp.equals(";")){
         nextToken = Symbols.SEMICOLON;
     }
     else if(temp.equals("const")){
         nextToken = Symbols.CONSTANT;
4

3 回答 3

0
Scanner scanner = new Scanner(fileName);
while (scanner.hasNextLine()) {
    String[] tokens = scanner.nextLine().split("\\s");
    //do what you want to do with the tokens
}
scanner.close();
于 2013-11-05T20:31:08.033 回答
0

请参阅StringTokenizer

我正在使用的 Class ctor:

StringTokenizer(String str, String delim)

一个例子:

 StringTokenizer st = new StringTokenizer("this is a test", "$a; ");
 while (st.hasMoreTokens()) {
     System.out.println(st.nextToken());
 }

在这里你可以找到一堆例子。

于 2013-11-05T20:32:23.740 回答
0

试试这样:

BuffereReader reader = new BufferedReader(**YourInputStream**);
String line = reader.readLine();

String[] tokens = line.split("\\s"); 
于 2013-11-05T20:32:51.230 回答