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 771517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:39:59+00:00 2026-05-14T18:39:59+00:00

I’m not a java programmer, I’m a VB programmer. I am doing this as

  • 0

I’m not a java programmer, I’m a VB programmer. I am doing this as part of an assignment, however, I’m not asking for help on something assignment related. I’d like to figure out how to get the OutputStreamWriter to work properly in this instance. I just want to capture the values I’m generating and place them into a text document. The file is generated, but only one entry exists, not the 40 I’m expecting. I could do this in a heartbeat with VB, but java feels very strange to me right now. Your help is appreciated.

Thanks,

Steve

Here’s the code:

  public static void main(String[] args) {
    long start, end;
    double result,difference;

    try {
      //OutputStream code assistance from 
      // http://tutorials.jenkov.com/java-io/outputstreamwriter.html
      OutputStream outputStream = new FileOutputStream("c:\\Temp\\output1.txt");
      Writer out = new OutputStreamWriter(outputStream);

      for(int n=1; n<=20; n++) {
        //Calculate the Time for n^2.
        start = System.nanoTime();

        //Add code to call method to calculate n^2
        result =  mN2(n);
        end = System.nanoTime();
        difference = (end - start);

        //Output results to a file
        out.write("N^2 End time: " + end + " Difference: " + 
            difference + "\n");
        out.close();
      }
    } catch (IOException e){
    }

    try {
      OutputStream outputStream = new FileOutputStream("c:\\Temp\\output1.txt");
      Writer out = new OutputStreamWriter(outputStream);

      for(int n=1; n<=20; n++){
        //Calculate the Time for 2^n.
        start = System.nanoTime();
        //Add code to call method to calculate 2^n
        result =  m2N(n);
        end = System.nanoTime();
        difference = (end - start);
        //Output results to a file
        out.write("N^2 End time: " + end + " Difference: " + difference + "\n");
        out.close();
      }
    } catch (IOException e){
    }
  }

  //Calculate N^2
  public static double mN2(double n) {
    n = n*n;
    return n;
  }

  //Calculate 2N
  public static double m2N(double n) {
    n = 2*n;
    return n;
  }
  • 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-14T18:39:59+00:00Added an answer on May 14, 2026 at 6:39 pm

    You’re closing your file within the loop. The next time around the loop you will attempt to write to the closed file, which will throw an exception…but where you catch IOException you have an empty block, which will effectively ignore the exception.

    Trying moving the out.close() call into a finally block, like this:

    try {
      ...
    }
    catch ( IOException e) {
      // Log any errors
    }
    finally {
      out.close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 416k
  • Answers 416k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The result of shell-command is the status. So don't compare… May 15, 2026 at 9:15 am
  • Editorial Team
    Editorial Team added an answer You can use a JavaScript validator. For example, function limitText(limitField,… May 15, 2026 at 9:15 am
  • Editorial Team
    Editorial Team added an answer Use the BitmapFactory to get your image from file, or… May 15, 2026 at 9:15 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.