1

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?

  • possible duplicate of [Working with large text snippets in Java source](http://stackoverflow.com/questions/782810/working-with-large-text-snippets-in-java-source) – Karoly Horvath Nov 02 '11 at 18:11
  • 1
    possible duplicate of [Java multiline string](http://stackoverflow.com/questions/878573/java-multiline-string) – skiphoppy Nov 02 '11 at 18:18

2 Answers2

2

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.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
0

In the meantime if you find it useful here's a basic swing application I use to do this for SQL that's embedded into the classes. I find myself using it over and over again so why not share it?

https://github.com/danielbchapman/Swing-String-Escaping-Utility

(a small note, its throw away code since I put it together in a week where I needed to rapidly format queries, I might clean it up at some point)

Daniel B. Chapman
  • 4,647
  • 32
  • 42