我试图将file.txt逐行读入java,然后当一行是“foo”时,我将它后面的行设置为“lineAfterFoo”,然后将其输出给用户。
我的 Java 代码....
public void main(String[] args) throws IOException {
try {
FileReader someFile = new FileReader("file.txt");
BufferedReader input = new BufferedReader(someFile);
int i = 0;
String[] line;
line = new String[10];
line[i] = input.readLine();
while(line[i] != null) {
line[i] = input.readLine();
if (line[i] == "foo") {
i = i + 1;
line[i] = "lineAfterFoo";
}
i = i + 1;
}
for (int number = 1; number < i; number++) {
System.out.println(line[number]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
文件.txt
1
2
3
foo
HopeFullyThisWillChange
5
6
7
8
9
10
错误...
java.lang.NoSuchMethodError: main
Exception in thread "main"
谢谢你的帮助!