我目前正在通过令牌读取文件令牌,我想首先读取整行,然后将其拆分为令牌。这是我通过令牌阅读的代码的一部分:
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;
答案 0 :(得分: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();
答案 1 :(得分:0)
请参阅StringTokenizer:
我正在使用的班级人员:
StringTokenizer(String str, String delim)
一个例子:
StringTokenizer st = new StringTokenizer("this is a test", "$a; ");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
Here你可以找到很多例子。
答案 2 :(得分:0)
尝试:
BuffereReader reader = new BufferedReader(**YourInputStream**);
String line = reader.readLine();
String[] tokens = line.split("\\s");