java覆盖txt,在java中覆盖txt文件

The code I've written is supposed to overwrite over the contents of the selected text file, but it's appending it. What am I doing wrong exactly?

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

{

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();

}

EDIT

I tried making a new temp.txt file and writing the new contents into that, deleting this text file and renaming temp.txt to this one. Thing is, the deletion is always unsuccessful. I don't think I have to change user permissions for this do I?

Also, a part of my program lists all the files in this directory, so I'm guessing they're being used by the program and so can't be deleted. But why not overwritten?

SOLVED

My biggest "D'oh" moment! I've been compiling it on Eclipse rather than cmd which was where I was executing it. So my newly compiled classes went to the bin folder and the compiled class file via command prompt remained the same in my src folder. I recompiled with my new code and it works like a charm.

File fold=new File("../playlist/"+existingPlaylist.getText()+".txt");

fold.delete();

File fnew=new File("../playlist/"+existingPlaylist.getText()+".txt");

String source = textArea.getText();

System.out.println(source);

try {

FileWriter f2 = new FileWriter(fnew, false);

f2.write(source);

f2.close();

} catch (IOException e) {

e.printStackTrace();

}

解决方案

SOLVED

My biggest "D'oh" moment! I've been compiling it on Eclipse rather than cmd which was where I was executing it. So my newly compiled classes went to the bin folder and the compiled class file via command prompt remained the same in my src folder. I recompiled with my new code and it works like a charm.

File fold=new File("../playlist/"+existingPlaylist.getText()+".txt");

fold.delete();

//File temp=new File("../playlist/temp.txt");

File fnew=new File("../playlist/"+existingPlaylist.getText()+".txt");

String source = textArea.getText();

System.out.println(source);

/*FileWriter f2;

PrintWriter pw;

try {

f2 = new FileWriter(fnew,false);

pw= new PrintWriter(f2);

f2.write(source);

/*for (int i=0; i

{

if(source.charAt(i)=='\n')

f2.append(System.getProperty("line.separator"));

f2.append(source.charAt(i));

}*/

try {

FileWriter f2 = new FileWriter(fnew, false);

f2.write(source);

f2.close();

//fnew.renameTo(fold);

//fold.renameTo(temp);

//temp.deleteOnExit();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值