Ignore a line while reading a file in java

前端 未结 3 564
粉色の甜心
粉色の甜心 2021-01-27 00:42

I have some code to read the lines from a file, I would like to recognize when the line starts or the fisrt character (not blank) is \'*\' and ignore it, so inside

3条回答
  •  清酒与你
    2021-01-27 01:26

    Change your while loop as:

    while((line = input.readLine()) != null){
       if(!line.startsWith("*")){
          String[] words = line.split(" ");
          ....
       }
    }
    

    EDIT

    If "*" is not in the beginning of the line but at some position in the line, use the following

    if(line.indexOf("*") == position){
       ......
    }
    

    where position can be an integer specifying the position you are interested in.

提交回复
热议问题