[英]Ignore a line while reading a file in java
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 the while statement add something like 我有一些代码可以从文件中读取行,我想识别行何时开始或fisrt字符(非空白)为'
*
'并忽略它,因此在while语句中添加如下内容
if(line[0]=='*')
ignore that line
I have something like: 我有类似的东西:
input = new BufferedReader(new FileReader(new File(finaName)));
String line = null;
while ((line = input.readLine()) != null) {
String[] words = line.split(" ");
....
}
How to complete the code? 如何完成代码?
input = new BufferedReader(new FileReader(new File(finaName)));
String line = null;
while ((line = input.readLine()) != null) {
if(line.trim().indexOf('*') == 0)
continue;
String[] words = line.split(" ");
....
}
Change your while loop as: 将while循环更改为:
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. 其中position可以是一个整数,指定您感兴趣的位置。
Assuming the * marks and end of line comment, this loop will keep anything needed on the line before it. 假定带有*标记和行尾注释,此循环将保留行中任何需要的内容。
input = new BufferedReader(new FileReader(new File(finaName)));
String line = null;
char[] lineChars;
while ((line = input.readLine()) != null) {
lineChars = line.toCharArray();
line = "";
for(char c: lineChars){
if(c == '*')break;
line.concat(Character.toString(c));
}
if(!line.equals(""))
String[] words = line.split(" ");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.