代码之家  ›  专栏  ›  技术社区  ›  Jason

如何删除文件中的现有文本

  •  7
  • Jason  · 技术社区  · 15 年前

    我正在为学校做一项作业,并在努力获得额外的学分。该程序将演示给定整数数组大小的线性和二进制搜索之间的效率差异。我建立了一个循环,创建一个int[size]数组,搜索一个随机数,然后创建一个int[size*2]的新数组。

    然后将结果写入文本文件。输出写得很好,但是在编译和运行程序几次之后,输出文件就有那么多的数据段。

    这是嵌套在try/catch块中的代码:

    File output= new File("c:\\BigOhResults.txt");
    int counter=2;
    
    if (output.canWrite() && output.exists()) {
        BufferedWriter out= new BufferedWriter(new FileWriter(output, true));
        out.write(type+" \n\n"); //writes the search type
        out.write(type+"Search Results\n\n");
    
        while (counter <= data.size()) {
            out.write(data.get(counter-1)+" millisecond runtime " +
            "for a "+ data.get(counter-2)+" random number " +"sample size\n");
            counter=counter+2;
        }
    }
    

    每次运行程序时,是否有任何方法可以清除该输出文件中的文本?

    我这样做的原因是教授要求打印结果和数据图表。我已经完成了绘图要求,而且它工作得很好。我只想让文件打印输出与图形打印输出匹配。

    4 回复  |  直到 10 年前
        1
  •  3
  •   Turing    15 年前

    如前所述,[filewriter][1]构造函数允许您指定清除现有文本并从文件开头开始。关于代码的其他一些注释:

    • 在调用output.canwrite()后,对output.exists()的检查是多余的,不需要。output.can write()将检查文件是否存在,以及您是否可以写入该文件。
    • 不要忘记关闭BufferedWriter对象。

    像这样:

    if (output.canWrite()) {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new FileWriter(output, false));
            out.write(type+" \n\n"); //writes the search type
            out.write(type+"Search Results\n\n");
    
            while (counter <= data.size()) {
                out.write(data.get(counter-1)+" millisecond runtime " +
                  "for a "+ data.get(counter-2)+" random number " +"sample size\n");
                counter=counter+2;
            }
        } catch (IOException e) {
            // Do what you want here, print a message or something
        } finally {
            if(out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    // Again, do what you want here
                }
            }
        }
    
    }
    

    [1]: http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileWriter.html#FileWriter(java.io.File ,布尔型)

        2
  •  5
  •   Paul Tomblin    15 年前

    FileWriter构造函数的第二个参数是“append”,它将传入“true”。也就是说,因为您已经将其设置为true,所以它会将新的输出附加到文件的末尾。如果您改为传入false,它将擦除已经存在的数据并将其写入新的数据。

        3
  •  4
  •   Charlie Salts    15 年前

    阅读文档 FileWriter . 你做 想追加。

        4
  •  0
  •   Ramprasad Dutt    10 年前

    好吧,所以我尝试了一些方法,但效果很好。这是当您有一个文件打算附加任何新的文本,但您希望在程序执行开始时重置/删除该文件的内容。希望我说得通。

        PrintWriter pw1 = new PrintWriter(new BufferedWriter(new FileWriter("newfile.txt")));
        pw1.close(); // Make sure the first PrintWriter object name is different from the second one.
    
        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("newfile.txt", true))); // PrintWriter in append-mode. When you recreate the text file with the same name, the file contents are erased because the previous object was not in append mode.
        pw.close();