title: Reading 1/n chunk of text input at one time using InputStream in Java
body: <p>I want to read a text file and insert   tag every 1/n chunk of text. For example, if the text is d</p>
  
  <pre><code> Hello world
   my name is hello kitty
   I am bored 
  </code></pre>
  
  <p>and if n = 3, I want the output to be like:</p>
  
  <pre><code>&lt;Tile&gt; Hello World &lt;/Tile&gt;
  &lt;Tile&gt; my name is ... &lt;/Tile&gt;
  &lt;Tile&gt; I am bored &lt;/Tile&gt;
  </code></pre>
  
  <p>I want the length of the characters in each tile approximately the same. So I want I did was this: </p>
  
  <pre><code>   int tileSize = 5; 
     File file = new File(inputDir + filename);
     BufferedFileWriter bw = new BufferedFileWriter(new FileWriter(outputDir + filename);
     long totalLength = file.length();
     int tileLength = (int)(totalLength/(double)tileSize);
     byte[] b = new byte[tileLength];
     InputStream in = new FileInputStream(file);
     while(in.read(b) != -1) {
            bw.write("&lt;TILE&gt;" + Charset.defaultCharset().decode(ByteBuffer.wrap(b)) + "&lt;/TILE&gt;");
     }
     bw.close(); 
  </code></pre>
  
  <p>I am doing this for multiple files (ex, 150 files), but this operation is somehow extremely slow. Something seems to be very inefficient. Could you give me any guidance in what might have caused this code to be slow or any better suggestion? Thanks! </p>