Java - 如何清除文本文件而不删除它? [英] Java - How to Clear a text file without deleting it?
本文介绍了Java - 如何清除文本文件而不删除它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
f = new Formatter(jibberish.txt);
s =新的扫描仪(jibberish.txt);
如果不存在。但是如果存在,我每次运行该程序时都要清除它?这就是我想知道的:再说一遍,如何清除已经存在的文件只是空白?
这是我在想什么:
pre $ public $ clearFile(){
//通过(s.hasNext()){
f.format();
$ div $解析方案
如果你想清除文件而不删除可能是你可以解决这个问题$ / $>
public static void clearTheFile(){
FileWriter fwOb = new FileWriter(FileName,false);
PrintWriter pwOb = new PrintWriter(fwOb,false);
pwOb.flush();
pwOb.close();
fwOb.close();
编辑:抛出异常,所以需要捕获异常
I am wondering what the best way to clear a file is. I know that java automatically creates a file with
f = new Formatter("jibberish.txt");
s = new Scanner("jibberish.txt");
if none already exists. But what if one exists and I want to clear it every time I run the program? That is what I am wondering: to say it again how do I clear a file that already exists to just be blank?
Here is what I was thinking:
public void clearFile(){
//go through and do this every time in order to delete previous crap
while(s.hasNext()){
f.format(" ");
}
}
解决方案 If you want to clear the file without deleting may be you can workaround this
public static void clearTheFile() {
FileWriter fwOb = new FileWriter("FileName", false);
PrintWriter pwOb = new PrintWriter(fwOb, false);
pwOb.flush();
pwOb.close();
fwOb.close();
}
Edit: It throws exception so need to catch the exceptions
这篇关于Java - 如何清除文本文件而不删除它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文