title: Create a random txt file of approximate size in Java
body: <p>OK, I have found the following code somewhere that generate a random txt file. Basically I want random words separated by some whitespace in order to run MapReduce word counting simulations. </p>
  
  <pre><code>import java.io.IOException;
  import java.io.PrintWriter;
  import java.util.Random;
  
  public class MainClass {
  
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
  
  
          try{
              PrintWriter writer = new PrintWriter("bigfile.txt", "UTF-8");
  
  
              Random random = new Random();
              for(int i = 0; i &lt; 23695522; i++)
              {           
                  char[] word = new char[random.nextInt(8)+3]; // words of length 3 through 10. (1 and 2 letter words are boring.)
                  for(int j = 0; j &lt; word.length; j++)
                  {
                      word[j] = (char)('a' + random.nextInt(26));
                  }
                  writer.print(new String(word) + ' ');
  
                  if (i % 10 == 0){
                      writer.println();
                  }
              }
  
  
              writer.close();
          } catch (IOException e) {
             // do something
          }
  
      }
  
  }
  </code></pre>
  
  <p>Now I want to alter this code a bit in order to have as much iterations as needed for the file to have approximately a predefined size. So, every iteration produces about 6.5 characters (due to uniform selection) each of 2 bytes. So, I divide the size of file I want in bytes by (6.5*2), set the result as the number of for loop iteration and get a file much smaller than I expect it to be.</p>