我有一个文本文件:
Filename: apple.jpg
Name: Apple
Brief: Apple is a fruit
Filename: orange.jpg
Name: Orange
Brief: Orange is also a fruit
Filename: tomato.jpg
Name: Tomato
Brief: Tomato is not a fruit, it's a vegetable
我有一个代码:
public class Test
{
public static void main(String[] args) throws IOException{
Scanner reader = new Scanner(new File("C:/textLocation.txt"));
String filename = "";
String name = "";
String brief = "";
String line = "";
while (reader.hasNextLine()){
line = reader.nextLine();
if (line.startsWith("Filename:") && (line.contains("apple"))){
filename = line.substring(10, line.length());
} else if (line.startsWith("Name:")){
name = line.substring(6, line.length());
} else if (line.startsWith("Brief:")){
brief = line.substring(7, line.length());
}
}
System.out.println(filename);
System.out.println(name);
System.out.println(brief);
}
}
我遇到的问题是当我设置苹果时,它将文件名设为 apple.jpg,这是正确的,但名称和简介是番茄。我该如何纠正?先感谢您。