Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 571665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:31:52+00:00 2026-05-13T13:31:52+00:00

I’m working on an assignment for school, and am trying something beyond for extra

  • 0

I’m working on an assignment for school, and am trying something beyond for extra credit. The program is to demonstrate the efficiency differences between a linear & binary search for a given array size of integers. I have a loop set up that creates an int[size] array, searches for a random number, then creates a new array of int[size*2].

The results are then written to a text file. The output writes fine, but after compiling & running the program several times, the output file has that many sections of data.

This is my code that is nested inside a try/catch block:

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;
    }
}

Is there any way I can erase the text within that output file upon each time the program is run?

the reason I’m doing this is the professor requires the result printout with the data graphed. I have already completed the graphing requirement, and it works fine. I just want to have the file printout match the graph printout.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:31:53+00:00Added an answer on May 13, 2026 at 1:31 pm

    As already mentioned, the [FileWriter][1] constructor allows you to specify to clear the existing text and start at the beginning of the file. Some other remarks about the code:

    • The check on output.exists() is redundant after a call to output.canWrite() and isn’t needed. output.canWrite() will check if the file exists and that you can write to it.
    • Don’t forget to close the BufferedWriter object.

    Like so:

    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, boolean)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.