简体   繁体   中英

Splitting a line from a file into tokens in java

I am currently reading the file token by token and I want to read a whole line first and then split it up into tokens instead. This is part of the code where I am reading by token:

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;
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();

See StringTokenizer :

The Class ctor I am using:

StringTokenizer(String str, String delim)

An example:

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

Here you can find bunch of examples.

try so:

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

String[] tokens = line.split("\\s"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM