【问题标题】:Is there heredoc alternative in Java (heredoc as PHP)? [duplicate]Java中是否有heredoc替代品(heredoc as PHP)? [复制]
【发布时间】:2013-02-17 20:09:31
【问题描述】:

对于 JAVA 开发,我需要使用诸如“\r\t\n ”之类的字符串写入文件,因为从 Java 中我想编写一个 PHP 文件。如果您看不懂,请看以下示例:

BufferedWriter buffW = new BufferedWriter(fileW);
            buffW.write("<?php\n\n\tclass MyClass {\n\tpublic function test()\n}\n}\n?>");

这是一团糟的代码,我想写一个干净的,比如 PHP 可以做的,但在 Java 上找不到替代方式作为例子:

 <?php
$mystring = <<<EOT
    This is some PHP text.
    It is completely free
    I can use "double quotes"
    and 'single quotes',
    plus $variables too, which will
    be properly converted to their values,
    you can even type EOT, as long as it
    is not alone on a line, like this:
EOT;
?> 

【问题讨论】:

  • 编写一个永不改变且只做一件事的 PHP 类的实用性是什么?您是否有更深入的示例将代码动态地放入 PHP 中?例如,如果我想在文件中添加一个函数——如果将该逻辑或方法分解为一个方法,这将比将长字符串放入 Java 中更容易和更具可读性。
  • 有时候我很想拥有这个功能。大多数情况下,当我在进行测试并且不想仅仅为了测试一个简单的解析器而处理文件 I/O 时。
  • Java multiline string 可能重复多行,stackoverflow.com/questions/3034186/… 不转义引号
  • 以及带有多行和引号转义的显式heredoc请求:stackoverflow.com/questions/2678483/…
  • 这终于可以通过 Java 13 文本块实现了,请参见此处:openjdk.java.net/jeps/355

标签: java php readfile heredoc


【解决方案1】:

Java 中有 heredoc 等效项吗?

不,Java 编程语言中没有直接的heredoc 替代方案。

检查这个类似的question

【讨论】:

  • Java 13 现在支持类似于 heredoc 的文本块。
猜你喜欢
  • 2016-08-24
  • 2012-06-12
  • 2021-04-30
  • 2015-12-29
  • 2013-07-09
  • 2013-02-24
  • 2011-07-25
  • 2020-06-11
  • 1970-01-01
相关资源
最近更新 更多