【发布时间】:2020-08-30 13:22:12
【问题描述】:
我想追加到文件,如果它不为空;如果它是空的,就想写。下面是我的代码。 write 函数有效, append 无效。有人可以在这里指导吗?
public class Filecreate {
public static void main(String args[]) throws IOException {
File file = new File("newFileCreated.txt");
System.out.println("file path "+file.getAbsolutePath() +" file length - "+file.length());
FileWriter myWriter = new FileWriter(file);
if((int)file.length() != 0){
myWriter.append("appended text\n");
}else{
myWriter.write("Files in Java might be tricky, but it is fun enough!");
}
myWriter.close();
System.out.println("file length after writing to file "+file.length());
}
}
【问题讨论】: