我有一个数组
arr[] = {"a", "b", "c"}
和
下面的文本文件1
2
3
4
5
我想编写一个将在
下面生成的循环1
2
3
4
5
Hello from a
1
2
3
4
5
Hello from b
1
2
3
4
5
Hello from c
下面是我试过的
String a;
for (int i =0; i<arr.length; i++) {
while (a = br.readLine() ){
fileWriter.write(a + "\n")
}
fileWriter.write("Hello from " + arr[i] +"\n");
}
我得到的是......
1
2
3
4
5
Hello from a
Hello from b
Hello from c
答案 0 :(得分:1)
我会首先读取整个文件,将其存储在内存中并进行循环。正如@AntonH所说,br(我想是一个BufferReader)会在一段时间内到达文件的末尾而你不会再将其指向开头。
String a;
String b = "";
while (a = br.readLine() ){
b += a + "\n"
}
for (int i =0; i<arr.length; i++) {
fileWriter.write(b + "Hello from " + arr[i] + "\n");
}