2
以下は私の現在の機能です。 lineToCompareが見つかった場合は正しく動作し、trueを返します。Java - ファイルからの線の取得
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;
}
ただし、lineToCompareの後に行を取得する必要があります。ここで
は、例えば、テキスト・ファイルです:
xXPwnageLolXx
1
SomethingTest
0
AnotherSomething
3
私はそれが見つかった行の後に番号を返し、その後、テキスト(lineToCompare)のラインを見つける必要があります。どうすればいい?
このスニペットを少し読みやすくするために、compareToの代わりに.equalsを使用できることを付け加えておきます。 –
while((strLine = br.readLine())!= null){ //行を比較する行と比較する(文字列) if(strLine.trim()。compareTo(lineToCompare)== 0) return new整数(br.readLine()) } –
@DorwardVillaruzはい - この値を返すことを希望する場合は同意します。彼は他の何でもしたいと思うように私はそれを開いたままにした。 –