【发布时间】:2013-02-23 20:00:29
【问题描述】:
正如我的标题所说。我需要在文件中搜索字符串。找到后,我需要下一行。 是这样的文件:
你好
世界
当找到“hello”时,需要返回“world”。
File file = new File("testfile");
Scanner scanner = null;
try {
scanner = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (scanner != null) {
String line;
while (scanner.hasNextLine()) {
line = scanner.nextLine();
if (line == "hello") {
line = scanner.nextLine();
System.out.println(line);
}
}
}
它会读取文件,但找不到单词“hello”。
【问题讨论】:
标签: java string file string-comparison