简体   繁体   中英

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?

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM