简体   繁体   English

在Java中我怎么能在这里做文档? 像BASH或PHP我们可以

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

How can i do with Java. 我该如何使用Java。 Here doc like string? 这里的doc像字符串? Exaple: 的exaple:

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

EOF;

System.out.println(java); 的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. 不幸的是,Java(截至7)不支持HERE文档(也称为多行字符串)。

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; 这些与Perl或PHP中的HERE文档不完全相似,因为描述模板的字符串不直接在您的代码中; 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. Stephen Colebourne提出了一个建议 ,以及通过Project Coin 提出了一个建议 ,但都没有将其纳入Java 7,这有点令人失望。 Languages like Groovy and Scala, which also run on the JVM do support multiline strings. 同样在JVM上运行的Groovy和Scala之类的语言也支持多行字符串。

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. 同时,如果您发现它有用,那么这是一个基本的swing应用程序,我使用它来对嵌入到类中的SQL进行此操作。 I find myself using it over and over again so why not share it? 我发现自己一遍又一遍地使用它,为什么不分享呢?

https://github.com/danielbchapman/Swing-String-Escaping-Utility 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) (请注意,由于我在需要快速格式化查询的一周内将它们放在一起,所以将其丢弃了代码,有时可能会清理掉它)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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