0

I am trying to benchmark sorting methods. My writeCSV(String) method writes over the first line every time I call it. Here is my code:

public static void main(String[] args) throws Exception{

    writeCSV("data size (100 times),bubble,insertion,merge,quick");

    sortRandomSet(20);
}

 public static void sortRandomSet(int setSize) throws Exception
{
    .
    .
    .
    writeCSV(setSize+","+bTime+","+mTime+","+iTime+","+qTime);
}
/*******************************************************************************
* writeCSV(course[]) method
* Last edited by Steve Pesce 3/19/2014 for CSci 112
* Writes String to CSV
* 
*/
public static void writeCSV(String line) throws Exception {

    //create new File object 
    java.io.File courseCSV = new java.io.File("benchmark.csv");

    //create PrintWriter object on new File object
    java.io.PrintWriter outfile = new java.io.PrintWriter(courseCSV);

    outfile.write(line + "\n");

    outfile.close();
}//end writeCSV(String)

I want writeCSV to start on a new line every time it is called, is this possible to do?

user2809114
  • 97
  • 4
  • 15

4 Answers4

1

Yeah, right after your call function you can add a string to the first line, have you tried that?

Also, when you create a new file add "a" as an argument which stands for append.

Try using RandomAccessFile

Have a look at this, it should explain how to add things to selected line in a text file

Community
  • 1
  • 1
MrHaze
  • 3,786
  • 3
  • 26
  • 47
  • 1
    When you write to a file, add a line of code that write to the first line, have a look at the link i added, it should explain how – MrHaze Mar 31 '14 at 03:09
1

Use java.io.FileWriter instead:

java.io.FileWriter outfile = new java.io.FileWriter("benchmark.csv", true); //true = append
outfile.write(line+"\n");
AndrewWhalan
  • 417
  • 3
  • 12
1

You could just use the append method. This will append your input to the end of the file.

shree.pat18
  • 21,449
  • 3
  • 43
  • 63
0

You should use "append method for this"

Yogesh Nikam Patil
  • 1,192
  • 13
  • 18