title: Java 8 Base64 Encode (Basic) doesn't add new line anymore. How can I reimplement this?
body: <p>I essentially have the exact <strong>opposite</strong> problem as</p>
  
  <p><a href="https://stackoverflow.com/questions/20040539/new-line-appending-on-my-encrypted-string">new-line-appending-on-my-encrypted-string</a></p>
  
  <p>It seems like the old Java Base64 utility would always add new lines every 76 characters when returning a string, but using the following code, I don't get those breaks I need. </p>
  
  <pre><code>    Path path = Paths.get(file);
      byte[] data = Files.readAllBytes(path);
      String txt= Base64.getEncoder().encodeToString(data);
  </code></pre>
  
  <p>Is there an easy way to tell the encoder to add the newlines?</p>
  
  <p>I've tried implementing a stringbuilder to insert the newlines, But it ends up changing the entire output (I copy the text from java console to HxD editor, and compare against my known working 'BLOB' with newlines). </p>
  
  <pre><code>    String txt= Base64.getEncoder().encodeToString(data);
  
      //Byte code for newline
      byte b1 = 0x0D;
      byte b2 = 0x0A;     
  
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i &lt; txt.length(); i++) {
          if (i &gt; 0 &amp;&amp; (i % 76 == 0)) {
              sb.append((char)b1);
              sb.append((char)b2);
  
          }
  
          sb.append(txt.charAt(i));
      }
  </code></pre>
  
  <p>EDIT (in response to question):</p>
  
  <p>It's not the easiest thing to explain, but when I don't use string builder, the output of the encode will start like this:</p>
  
  <p>AAAAPAog4lBVgGJrT2b+mQVicHN3d////////3hhcDJiLWVtMjUwLWVtMjUwLWRldjA0NTUAAAAAAA</p>
  
  <p>But I want it to look like this:</p>
  
  <p>AAAAPAog4lBVgGJrT2b+mQVicHN3d////////3hhcDJiLWVtMjUwLWVtMjUwLWRldjA0NTUAAAAA..AA</p>
  
  <p>As you can see, the ".." represents 0x0D and 0X0A or  a newline, which is insterted at the 76th character (this is what the old base64 would output).</p>
  
  <p>However, when I append the bytes b1 and b2 (newline) after the 76th character, the output becomes:</p>
  
  <p>BPwAFHwA0CUFoG8AgDRCAAIlQgAAJUIAAhUfNEIAAiUkmw/0fADQFSInART/ADUlfADQFQE0fADQ..</p>
  
  <p>So it looks like the ".." is in the right spot, but everything before it is different. </p>
  
  <p>Thanks!</p>