title: Check file size while writing file and create new file
body: <p>I am trying to write java code to write &quot;.dat&quot; File. While writing, check size as well. Every 200 KB create new &quot;.dat&quot; file and continue to write file. Is there any way that I can do that?</p>
  <p>When file is bigger than 200KB, create a new one like this:</p>
  <p>1st file name: 1_filename <br/>
  2nd file name: 2_filename <br/>
  3rd file name: 3_filename <br/>
  ... <br/>
  or if only one file is created (meaning it is smaller than 200KB) then:</p>
  <p>file name: filename</p>
  <p>I wrote the code, but it's only writing 2 files, so that's not the right way to do it.</p>
  <p>Here is my code:</p>
  <pre><code>import java.io.BufferedWriter;
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.nio.file.Files;
  import java.nio.file.Path;
  import java.nio.file.Paths;
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  import java.util.Calendar;
  
  public class xmlfile1filebytes {
  
    public static void main(String[] args) throws IOException {
      
      File folder = new File (&quot;07072013&quot;);
      File[] listOfFiles = folder.listFiles();
      
      System.out.println(&quot;There are &quot; + listOfFiles.length + &quot; files&quot;); 
      String filesin;
      
      String timeStamp = new SimpleDateFormat(&quot;MM-dd-yyyy[HH.mm.ss]&quot;)
       .format(Calendar.getInstance().getTime());
       System.out.println(timeStamp);
       
      BufferedWriter xmlfile = null;
      BufferedWriter datfile = null;
    
      String outxmlfile = (&quot;07072013\\&quot; + timeStamp + &quot;.xml&quot;);
      xmlfile = new BufferedWriter(new FileWriter(outxmlfile));
      
      String outdatfile = (&quot;07072013\\&quot; + timeStamp + &quot;.dat&quot;);
      datfile = new BufferedWriter(new FileWriter(outdatfile));
      
      int offset = 0;
      int size = 0;
  
      for (int i = 0; i &lt; listOfFiles.length; i++) {
         
          File f = listOfFiles[i];
          
         // System.out.println(i + &quot; &quot; + f.getAbsolutePath());
          if (f.isFile()) {
  
              filesin = listOfFiles[i].getName();
  
              if (filesin.endsWith(&quot;pdf&quot;)) {
                 
                  Path aPath = Paths.get(f.getAbsolutePath()); 
         
                  System.out.println(filesin);
                  
                  byte[] actualBytes = Files.readAllBytes(aPath);
                  size = actualBytes.length;
               
                  xmlfile.append((i + 1) + &quot;)&quot; + &quot; File = &quot; + filesin + &quot;, Offset = &quot; + offset + &quot;, Size = &quot; + size + &quot;\n&quot;);
                 
             
                  offset = offset + size;
                  xmlfile.newLine();
              
                  String s = new String(actualBytes);
              
                  datfile.append(s);
                  datfile.newLine();
                  
                  
                  File datfileinfolder = new File (&quot;07072013\\&quot; + timeStamp + &quot;.dat&quot;);
                  
                  long datfilesize = datfileinfolder.length();
                  long datfilesizeinkb = datfilesize /1024;
                  
                  System.out.println(datfilesizeinkb);
                  
                  if (datfilesizeinkb &gt;= 200) {
                       datfile.close();
  
                        BufferedWriter datfile1 = null;
                        String outdatfile1 = (&quot;07072013\\&quot; + &quot;1_&quot;+ timeStamp + &quot;.dat&quot;);
                        datfile1 = new BufferedWriter(new FileWriter(outdatfile1));
                       
                        String s1 = new String(actualBytes);
                        datfile1.append(s1);
                        datfile1.close();
                  }
                  
                  
               }
          }
      }
      
       xmlfile.close();
    }
  }
  </code></pre>
  <p>And I get an error when the file is bigger than 400 KB.</p>
  <p>ERROR:</p>
  <pre><code>There are 10 files
  07-09-2013[16.03.00]
  1192970_eBill_20130709.pdf
  96
  1321470_eBill_20130709.pdf
  208
  1724897_eBill_20130709.pdf
  Exception in thread &quot;main&quot; java.io.IOException: Stream closed
      at java.io.BufferedWriter.ensureOpen(Unknown Source)
      at java.io.BufferedWriter.write(Unknown Source)
      at java.io.Writer.write(Unknown Source)
      at java.io.Writer.append(Unknown Source)
      at xmlfile1filebytes.main(xmlfile1filebytes.java:65)
  </code></pre>