Search code examples
javaphplinuxbashbash4

In Java how can i do here docs? Like BASH or PHP we can


How can i do with Java. Here doc like string? Exaple:

String java = << \EOF
#This file is written via Java
#You are watching JavaHereDoc
; comments ;
value=abc
etc etc

EOF;

System.out.println(java); shows exactly like above. How can i do this?


Solution

  • Java (as of 7) doesn't support HERE docs (also known as multiline strings) unfortunately.

    If you're trying to accomplish templating, there are a few options:

    These aren't exactly similar to HERE docs in Perl or PHP since the string that describes the template isn't directly in your code; it's usually in a separate file.

    There was a proposal put forward by Stephen Colebourne as well as a proposal via Project Coin, neither of which made it into Java 7, which was a little disappointing. Languages like Groovy and Scala, which also run on the JVM do support multiline strings.