【问题标题】:Trying to write the data to a .txt file. What am I doing wrong?试图将数据写入 .txt 文件。我究竟做错了什么?
【发布时间】:2015-11-29 22:40:12
【问题描述】:

我想将数据(函数运行所需的时间)写入 .txt 文件,以便我可以将其带到 Excel 并制作电子表格(可以在电子表格)。

这是我目前所拥有的: 计时.java

  public static void main(String args[]) {

        Stopwatch timeMe = new Stopwatch();

    ...

    try
    {
        File logFile = new File("log.txt");
        FileWriter fw = new FileWriter(logFile2, true);
        FileWriter fw2 = new FileWriter(logFile, true);
        BufferedWriter bw = new BufferedWriter(fw);

        // if file doesnt exists, then create it
        if (!logFile.exists()) {
            logFile.createNewFile();
            System.out.println("doesn't exist");
        }
    ...

        for (int i = 1000; i <= 256000; i+=1000)
        {
            int[] randomArray = randomarr(i);
            fw.write(String.valueOf(i));
            fw.write(String.format("%n"));
            timeMe.start();
            pluto(randomArray);
            timeMe.stop();
            fw.write(timeMe.toString());
            fw.write(String.format("%n"));
        }
    }

    catch (IOException e)
    {
        e.printStackTrace();
    }
...
}

Stopwatch.java

/**
 A class to measure time elapsed.
*/

public class Stopwatch
{
    private long startTime;
    private long stopTime;

    public static final double NANOS_PER_SEC = 1000000000.0;

    /**
     start the stop watch.
    */
    public void start()
    {   System.gc();
        startTime = System.nanoTime();
    }

    /**
     stop the stop watch.
    */
    public void stop()
    {   stopTime = System.nanoTime();   }

    /**
    elapsed time in secods.
    @return the time recorded on the stopwatch in seconds
    */
    public double time()
    {   return (stopTime - startTime) / NANOS_PER_SEC;  }

    public String toString()
    {   return "elapsed time: " + time() + " seconds."; }

    /**
    elapsed time in nanoseconds.
    @return the time recorded on the stopwatch in nanoseconds
    */
    public long timeInNanoseconds()
    {   return (stopTime - startTime);  }
}

问题是,当我在秒表中更改任何内容时,即删除“经过时间:”和“秒”。在 .toString() 方法中隔离数字,不会打印到文件中。换句话说,.txt 文件是空的。当我使用原始的 Stopwatch.java 文件运行它时,所有内容都会打印到 log.txt 文件中,但我不想要这个文本。

【问题讨论】:

标签: java file


【解决方案1】:

您是否关闭文件和/或flush it explicitly?否则,您的数据可能会在程序终止时被缓冲但未写入。

它可能一直有效,直到您删除周围的字符,这仅仅是因为您已经生成了足够的文本来自动触发文件写入。

相关:BufferedWriter not writing everything to its output file

【讨论】:

    【解决方案2】:
    FileWriter fw2 = new FileWriter(logFile, true);
    

    在这里,您创建了一个名为 what is in logFile 的文件。

    // if file doesnt exists, then create it
    if (!logFile.exists()) {
        logFile.createNewFile();
    
        System.out.println("doesn't exist");
    }
    

    在这里你已经用另一个覆盖了它。如果您使用的是 Unix、Linux 等,fw2 将继续写入前一个文件,但它已被删除,因此您将永远看不到数据。删除整个块。

    【讨论】:

    • 这绝对是一个很好的建议,但我确实看到 OP 使fw2 (logFile) 指向的文件成为孤儿,而不是fw(他写道)。无论哪种方式,我都不确定 toString() 方法中的孤立更改会如何影响它。
    猜你喜欢
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 2019-12-23
    • 2011-04-16
    • 2014-06-15
    相关资源
    最近更新 更多