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

  • SEARCH
  • Home
  • 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 7970829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:29:33+00:00 2026-06-04T07:29:33+00:00

This method should write random chars, but it doesn’t write anything at all. I’m

  • 0

This method should write random chars, but it doesn’t write anything at all. I’m probably doing something stupidly wrong here, but for the life of me I can’t find it.

public void writeRandomChunk(String fileName) {
    try {
        File saveFile = new File(folderName + '/' + fileName);

        PrintWriter writer = new PrintWriter(
                             new BufferedWriter(
                             new FileWriter(saveFile)));

        Random r = new Random(System.currentTimeMillis());

        for (int i = 0; i < chunkSize; i++) {
            for (int j = 0; j < chunkSize; j++) {
                writer.print((char)(r.nextInt(26) + 'a'));
            }
            writer.println();
        }

    } catch (Exception e) {
        System.out.println("Error in WorldFile writeRandomFile:\n"
                           + e.getLocalizedMessage());
    }
}
  • 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-06-04T07:29:34+00:00Added an answer on June 4, 2026 at 7:29 am

    As with any stream (and this applies to most any language), you need to close it when you are done.

    Streams are optimized to be fast, and as a consequence, not all of the data you write to them instantly appears in the file. When you close() or flush() a stream, the data is written to the file (or whatever other storage mechanism you are using).

    In your case, try the following, instead.

    public void writeRandomChunk(String fileName) {
        PrintWriter writer = null;
        try {
            File saveFile = new File(folderName + '/' + fileName);
            writer = new PrintWriter(
                                 new BufferedWriter(
                                 new FileWriter(saveFile)));
    
            Random r = new Random(System.currentTimeMillis());
    
            for (int i = 0; i < chunkSize; i++) {
                for (int j = 0; j < chunkSize; j++) {
                    writer.print((char)(r.nextInt(26) + 'a'));
                }
                writer.println();
            }
    
        } catch (Exception e) {
            System.out.println("Error in WorldFile writeRandomFile:\n"
                               + e.getLocalizedMessage());
        } finally {
            if (writer != null)
                writer.close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have this method below which should insert values into my database. However
Should I be using this method of throwing errors: if (isset($this->dbfields[$var])) { return $this->dbfields[$var];
Should I always be using this method when rendering? Does it slow down much
i don't understand this example from the doc : the timeIntervalSinceNow method should show
i get this error when trying this: ERROR method name expected. How should i
I have this part in a method, It should return the page and headers
This method is working totally right in matlab. but, when I compiled it in
Given that NetworkStream.Write() is a blocking call should a method like SendMessage() create a
I wrote this method so that I dont have to write opening connection etc.
How can I write a method and force the subclasses to override this method.

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.