在java中覆盖txt文件 [英] Overwriting txt file in java

查看:130
本文介绍了在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<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重命名为此文件。事实是,删除总是不成功。我不认为我必须更改此用户权限吗?

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?

已解决

我最大的D'oh时刻!我一直在Eclipse而不是cmd上编译它,这是我执行它的地方。所以我新编译的类转到bin文件夹,通过命令提示符编译的类文件在我的src文件夹中保持不变。我用我的新代码重新编译它就像一个魅力。

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


推荐答案

已解决

我最大的D'oh时刻!我一直在Eclipse而不是cmd上编译它,这是我执行它的地方。所以我新编译的类转到bin文件夹,通过命令提示符编译的类文件在我的src文件夹中保持不变。我用我的新代码重新编译它就像一个魅力。

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<source.length();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();
            }   

这篇关于在java中覆盖txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆