java - How to erase existing text in a file -


I am working on an assignment for school, and am trying something beyond additional credit. The program is for displaying the difference of efficiency between a linear amp; Binary search for a given array size of an integer I have a loop set up which creates an int [size] array, searches for a random number, then creates a new array of int [size * 2] is.

The result then a text file. Output ok, but after compiling & amp; While running the program several times, the output file contains several sections of the data.

This is my code that is nested in any attempt / catch block:

  file output = new file ("c: \\ BigOhResults.txt"); Int counter = 2; If (output. CanWrite () & output.exists ()) {BufferedWriter = New BufferedWriter (new FileWriter (output, true)); Out.written (type + "\ n \ n"); // typed the search type. Type (type + "search result \ n \ n"); While (counter & lt; = data.size ()) {out.write (data.get (counter-1) + "millisecond runtime" + "for" + data.gate (counter 2) + "random number" + "Sample size \ n"); Counter = counter + 2; }}  

Is there a way that I can erase the text every time within that output file when the program is run?

The reason for this is that Professor requires printout of results with data print. I have already completed the need of graphing, and it works fine, I have to print with print printouts.

As already mentioned, [FileWriter] [1] the constructor will clean up the existing text Allows to specify and specify to start from the beginning of the file. Some other comments about code:

  • Check output on output.exists (). Call after canWrite () is unnecessary and it is not required. Output.canWrite () will check whether the file exists and you can write it.
  • Do not forget to close the BufferedWriter object

It is as follows:

  if (output. Canwrite ()) { Bufferbutt = exit; {Try outside = new buffed waist (new freeware (output, wrong)); Out.written (type + "\ n \ n"); // typed the search type. Type (type + "search result \ n \ n"); While (counter & lt; = data.size ()) {out.write (data.get (counter-1) + "millisecond runtime" + "for" + data.gate (counter 2) + "random number" + "Sample size \ n"); Counter = counter + 2; }} Hold (IOException E) {// What you want here, a message or something else} Finally {if (exit! =) Tap (try {out.close ()); } Hold (IOException e) {// Again, do whatever you want here}}}}  

[1]:, boolean)


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -