# JAVA PrintWriter question

**URL:** <https://forum.kirupa.com/t/java-printwriter-question/215493>\
**Category:** programming\
**Created:** [February 7, 2007, 6:36pm UTC](https://forum.kirupa.com/t/java-printwriter-question/215493 "2007-02-07T18:36:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beaulne](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/beaulne/32/3027_2.png) [@Beaulne](https://forum.kirupa.com/u/Beaulne)\
**Post date:** [February 7, 2007, 6:36pm UTC](https://forum.kirupa.com/t/java-printwriter-question/215493/1 "2007-02-07T18:36:52Z")

</div>

The question I have about PrintWriter is how do I use it in a loop.

What I would like to do is write to a text file 100 random ints but every time I put the output in a loop it writes nothing to to the file…

Here is my code with out the loop

import [java.io](http://java.io)._;  
import java.util._;

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

```
      // Creates the file and the PrintWriter
      File file = new File(args[0]);
      PrintWriter output = new PrintWriter(file);
      // The Random Int Generator
      Random generator = new Random();
      int randomInt = generator.nextInt();

      output.print(randomInt);
      // Close the file
      output.close();

    }
  }

```

here is the code I would think of using with a loop  
import [java.io](http://java.io)._;  
import java.util._;

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

```
      // Creates the file and the PrintWriter
      File file = new File(args[0]);
      PrintWriter output = new PrintWriter(file);
      // The Random Int Generator
      Random generator = new Random();
      int randomInt = generator.nextInt();

      int count =0;

      while (count ==100){
      output.print(randomInt);
      count++;
      }
      // Close the file
      output.close();

    }
  }

```

This prints the file but with nohting in it.

Thanks
