英文:
Unable to append to file based on file.length() operation
问题
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());
}
}
英文:
I want to append to the file and if its not empty; and want to write if its empty. Below is is my code. write function works, append is not. Can anyone guide here?
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());
}
}
答案1
得分: 1
问题发生的原因是您在打开文件后测量文件大小。因此,您必须在打开文件之前检查文件大小。此外,我不建议将 long 强制转换为 int,因为您的解决方案在处理大文件时可能不起作用。总之,以下代码将适用于您:
public static void main(String[] args) throws IOException {
File file = new File("newFileCreated.txt");
long fileSize = file.length();
System.out.println("文件路径:" + file.getAbsolutePath() + " 文件长度:" + file.length());
FileWriter myWriter = new FileWriter(file);
if (fileSize > 0L) {
myWriter.append("追加的文本\n");
} else {
myWriter.write("在Java中处理文件可能有些棘手,但足够有趣!");
}
myWriter.close();
System.out.println("写入文件后的文件长度:" + file.length());
}
英文:
The issue occurs because you measure file's size after you open it. Thus, you have to check file's size before you open it. Also, I won't recommend to cast long to int, because your solution won't work on big files. To conclude, following code will work for you:
public static void main(String[] args) throws IOException {
File file = new File("newFileCreated.txt");
long fileSize = file.length();
System.out.println("file path "+file.getAbsolutePath() +" file length - "+file.length());
FileWriter myWriter = new FileWriter(file);
if(fileSize > 0L){
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());
}
答案2
得分: 1
你无需担心文件是否包含任何内容。只需将参数true应用于FileWriter构造函数中的append参数,然后始终使用**Writer#append()**方法,例如:
String ls = System.lineSeparator();
String file = "MyFile.txt";
FileWriter myWriter = new FileWriter(file, true);
myWriter.append("appended text" + ls);
/* 立即将流写入文件。只有在写入循环中实际需要,且您希望立即查看写入结果时,才真正需要。
close()方法在关闭之前还会将流刷新到文件中。 */
myWriter.flush();
System.out.println("写入文件后的文件长度:" +
new File(file).length());
myWriter.close();
- 如果文件尚不存在,系统将自动创建它并将行追加到其中。
- 如果文件已创建但为空,则将行追加到其中。
- 如果文件包含内容,则仅将行追加到该内容中。
英文:
You don't need to worry about whether or not the file contains anything. Just apply the argument of true to the append parameter in the FileWriter constructor then always use the Writer#append() method, for example:
String ls = System.lineSeparator();
String file = "MyFile.txt";
FileWriter myWriter = new FileWriter(file, true)
myWriter.append("appended text" + ls);
/* Immediately write the stream to file. Only really
required if the writes are in a loop of some kind
and you want to see the write results right away.
The close() method also flushes the stream to file
before the close takes place. */
myWriter.flush();
System.out.println("File length after writing to file " +
new File(file).length());
myWriter .close();
- If the file doesn't already exist it will be automatically created
and the line appended to it. - If the file is created but is empty then the line is appended to it.
- If the file does contain content then the line is merely appended to
that content.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论