0

I want to write data- the time it takes for a function to run- to a .txt file so that I can take it to Excel and make a spreadsheet (it's possible to make log-log and semi-log graphs in Excel).

Here's what I have so far: timing.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);  }
}

The problem is, when I change anything in Stopwatch, i.e. remove the "elapsed time: " and " seconds." in the .toString() method to isolate the numbers, nothing will print to the file. In other words, the .txt file is empty. When I run it using the original Stopwatch.java file, everything is printed to the log.txt file, but I don't want this text.

hbd
  • 674
  • 7
  • 21
  • 1
    Writers MUST be closed. Read http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html – JB Nizet Sep 03 '15 at 23:10
  • Why are there two files? And why aren't you writing to the second one? And why have you mixed up the names so that `fw = logfile2` and *vice versa?* – user207421 Sep 04 '15 at 02:53

2 Answers2

2

Do you close the file, and/or flush it explicitly? If not, your data may be buffered but unwritten when your program terminates.

It may be working until you remove the surrounding characters simply because you've generated enough text to automatically trigger a file write.

Related: BufferedWriter not writing everything to its output file

Community
  • 1
  • 1
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
1
FileWriter fw2 = new FileWriter(logFile, true);

Here you have created a file called whatever is in logFile.

// if file doesnt exists, then create it
if (!logFile.exists()) {
    logFile.createNewFile();

    System.out.println("doesn't exist");
}

Here you have overwritten it with another one. If you're on Unix, Linux, etc., fw2 will continue to write to the previous file, but it has been deleted so you will never see the data. Remove this entire block.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • This is definitely good advice to follow, but I do see that the OP orphans the file pointed to by `fw2` (`logFile`) and not `fw` (to which he writes). Either way I'm not sure how this would be affected by an isolated change in a `toString()` method. – Jeff Bowman Sep 04 '15 at 00:40