0

a:b:c:d:e

bb:cc:dd:ee:ff

ccc:ddd:eee:fff:ggg

上記のテキストファイルの内容があります。ユーザー入力をテキスト ファイルと比較しようとしています。例えば

cc:dd

見つかったら、行全体を取得する必要があります。ユーザーが入力した行を取得するにはどうすればよいですか? 使用してみwhile(scanner.hasNext())ましたが、希望の結果が得られませんでした。

4

1 に答える 1

2

標準 Java ライブラリの場合:

File file = new File("file.txt");
String word = "abc";
Scanner scanner = null;

try {
    scanner = new Scanner(file);
} catch(FileNotFoundException e) { 
   //handle this
}

//now read the file line by line
while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    if(line.contains(word)) { 
        System.out.println(line);
    }
}
scanner.close();
于 2013-04-27T18:05:04.323 に答える