title: How can I preserve new line characters in the creation of TextFlow dynamically with FXML and JavaFX
body: <p>The following code dynamically creates a TextFlow and Text that originally has new line characters in it (potentially images and other panes).</p>
  
  <pre><code>InputStream stream = new ByteArrayInputStream(text.toString().getBytes(StandardCharsets.UTF_8));
        TextFlow element = new FXMLLoader().load(stream);
  </code></pre>
  
  <p>However, the ByteArrayInputStream method with FXML strips out all the new lines.</p>
  
  <p>Tried using both Windows&amp;Unix end of line characters, not that that should make a difference anyway, but you get to that point when you try different combinations in vain.</p>
  
  <p>Usually it's <a href="https://stackoverflow.com/questions/22021291/do-any-java-stream-input-libraries-preserve-line-ending-characters">recommended to use a BufferedReader</a> to get the new lines back, but here I'm not in control. The other load methods seem to take URL's and resource links, however this is a manipulated string.</p>
  
  <p><strong>An example</strong></p>
  
  <p>The java code:</p>
  
  <pre><code>package simpleexample;
  
  import java.io.ByteArrayInputStream;
  import java.io.InputStream;
  import java.nio.charset.StandardCharsets;
  
  import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Scene;
  import javafx.scene.layout.VBox;
  import javafx.scene.paint.Color;
  import javafx.scene.text.TextFlow;
  import javafx.stage.Stage;
  
  import com.jcabi.xml.XMLDocument;
  
  public class TestFxmlWithBreaks extends Application {
  
  
    @Override
    public void start(Stage primaryStage) {
      Scene scene = new Scene(createEntry(), 750, 750, Color.web("#666970"));
      primaryStage.setScene(scene);
      primaryStage.show();
  
    }
  
  
    private VBox createEntry() {
      VBox entryBox = new VBox();
      entryBox.setFillWidth(true);
  
      entryBox.setSpacing(20);
      entryBox.setMaxWidth(750);
  
      try {
        XMLDocument test = new XMLDocument(this.getClass().getResourceAsStream("inputFxml.xml"));
        System.out.println(test.toString());
  
        InputStream stream = new ByteArrayInputStream(test.toString().getBytes(StandardCharsets.UTF_8));
        TextFlow element = new FXMLLoader().load(stream);
        element.setMaxWidth(750);
  
        entryBox.getChildren().add(element);
      } catch (Exception e) {
        //log something
      }
  
      return entryBox;
    }
  
  
    public static void main(String[] args) {
    launch(args);
    }
  
  
  
  }
  </code></pre>
  
  <p>inputFxml.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;?import javafx.scene.text.*?&gt;&lt;TextFlow xmlns:fx="http://javafx.com/fxml" styleClass="paragraph"&gt;
  &lt;Text style="-fx-font-size: 20;"&gt;A list of stuff&lt;/Text&gt;
  &lt;Text&gt;
  * stuff
  * more stuff
  * even more stuff, all with new lines
  &lt;/Text&gt;
  &lt;Text style="-fx-font-size: 15;"&gt;This should demonstrate it&lt;/Text&gt;
  &lt;/TextFlow&gt;
  </code></pre>