以下是我目前的功能.它工作正常,如果找到lineToCompare则返回true.
public static int checkrank(String lineToCompare){
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("ranks.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
//Compare the line with the line to compare (string)
if(strLine.trim().compareTo(lineToCompare) == 0)
return true;
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
但是,我需要它来找到lineToCompare之后的行.
这是一个示例文本文件:
xXPwnageLolXx
1
SomethingTest
0
AnotherSomething
3
Run Code Online (Sandbox Code Playgroud)
我需要它来查找文本行(lineToCompare),然后返回找到的行后面的数字.我该怎么做?
while ((strLine = br.readLine()) != null) {
//Compare the line with the line to compare (string)
if(strLine.trim().compareTo(lineToCompare) == 0) {
//I found it! Read the next line...
final String lineAfter = br.readLine();
return Integer.parseInt(lineAfter);
}
}
Run Code Online (Sandbox Code Playgroud)
我已编辑此片段以包含Dowards建议.局部变量是不必要的,我只是为了便于阅读而留下它.
归档时间: |
|
查看次数: |
561 次 |
最近记录: |