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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:15:32+00:00 2026-05-12T20:15:32+00:00

In the following program, loop is iterating 1000 times, and I am writing all

  • 0

In the following program, loop is iterating 1000 times, and I am writing all the entries in a file using a FileWriter, but unfortunately programs ends up writing only 510(sometimes 415, sometimes 692, always less then 1000) entries in the file, but loop is iterating 1000 times.

import java.io.* ;
import java.util.*;

public class DemoWriter {

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

        List<String> receiverList = new ArrayList<String>() ;
        receiverList.add("abc@gmail.com") ;
        receiverList.add("pqr@ibibo.com") ;
        receiverList.add("xyz@gmail.com") ;

        FileWriter fw = new FileWriter("a.txt") ;
        BufferedWriter bw = new BufferedWriter(fw) ;

        int size = receiverList.size() ;

        String str ;
        int count = 0 ;
        for(int i = 1 ; i <= 1000 ; ++i){
            str = receiverList.get( (int) (Math.random() * size) ) + "\n" ;
            bw.write(++count + ".> " + str) ;
            System.out.print(count + ".> " + str) ;
        }
    }
}

Is this because of file size or something else???


Thnx for the quick response to all the nice people here. I have corrected my code (I just forgot to close the stream and now code working perfectly). As all pointed that i need to close the stream, but i am accepting BalusC as he was the first one who replied.

Nice to c u BalusC here.
Cheers 🙂

  • 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-12T20:15:32+00:00Added an answer on May 12, 2026 at 8:15 pm

    Indeed, you should always close the resources to flush everything and free up the resources. Explicitly flushing is not necessary as it’s usually implicitly done during closing. Just invoke Closeable#close() inside the finally block of a try-catch-finally block and you’re fine.

    Learn more at the Java IO tutorial.

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

Sidebar

Related Questions

I need to emulate a do-while loop in a Python program. Unfortunately, the following
Hey everyone, I am trying to run the following program, but am getting a
Consider the following simple C program that read a file into a buffer and
So if I am iterating using a foreach loop and I have a function
Reference Iterating arrays in a batch file I have the following: for /f "tokens=1"
The following code in my function (run in a loop) is causing my program
The following program is very simple: it outputs a single dot each half a
I have the following program: ~/test> cat test.cc int main() { int i =
Suppose I've got the following program: namespace ReflectionTest { public class Example { private
let us have a situation in which the following program prints some 10 lines

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.