user285594
user285594

Reputation:

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?

Upvotes: 1

Views: 1081

Answers (2)

Daniel B. Chapman
Daniel B. Chapman

Reputation: 4687

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)

Upvotes: 0

Vivin Paliath
Vivin Paliath

Reputation: 95518

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.

Upvotes: 2

Related Questions