Tag: 编写器

滚动文件实现

我总是很好奇如何在日志中实现滚动文件。 如何开始创建任何语言的文件编写类,以确保不超过文件大小。 我能想到的唯一可能的解决方案是: write method: size = file size + size of string to write if(size > limit) close the file writer open file reader read the file close file reader open file writer (clears the whole file) remove the size from the beginning to accommodate for new string to write write the new truncated […]

在java中覆盖txt文件

我写的代码应该覆盖所选文本文件的内容,但它会附加它。 我究竟做错了什么? File fnew=new File(“../playlist/”+existingPlaylist.getText()+”.txt”); String source = textArea.getText(); System.out.println(source); FileWriter f2; try { f2 = new FileWriter(fnew,false); f2.write(source); /*for (int i=0; i<source.length();i++) { if(source.charAt(i)=='\n') f2.append(System.getProperty("line.separator")); f2.append(source.charAt(i)); }*/ f2.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } 编辑 我尝试制作一个新的temp.txt文件并将新内容写入其中,删除此文本文件并将temp.txt重命名为此文件。 事实是,删除总是不成功。 我不认为我必须为此更改用户权限吗? 此外,我的程序的一部分列出了该目录中的所有文件,因此我猜测它们正在被程序使用,因此无法删除。 但为什么不覆盖? 解决了 我最大的“D’哦”时刻! 我一直在Eclipse而不是cmd上编译它,这是我执行它的地方。 所以我新编译的类转到bin文件夹,通过命令提示符编译的类文件在我的src文件夹中保持不变。 我用我的新代码重新编译,它就像一个魅力。 File fold=new File(“../playlist/”+existingPlaylist.getText()+”.txt”); […]