當我運行的代碼一切正常,但內容不寫入target.txt。請告訴我爲什麼下面的代碼不工作?
public class SrtExtractor {
public static void main(String[] args) throws IOException {
Path source = Paths.get("files/loremipsum.txt");
Path target = Paths.get("files/target.txt");
Charset charSet = Charset.forName("US-ASCII");
BufferedReader reader = Files.newBufferedReader(source, charSet);
BufferedWriter writer = Files.newBufferedWriter(target, charSet);
String temp;
ArrayList<String> list = new ArrayList<>();
while((temp = reader.readLine())!=null){
list.add(temp);
System.out.println(temp);
}
for(int i = 0; i<list.size(); i++)
{
writer.append(list.get(i));//why this line is not working???
}
}
}
嘗試writer.flush()和writer.close() – Gus
您離開'writer.close()'。 – PeterMmm
你有沒有費心去瀏覽調試器中的代碼? – OldProgrammer