22

For JAVA development I need writing to a files with strings like "\r\t\n <>", because from Java I want writing a PHP file. If you can't understand look at this example:

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

This is mess a code, I want to write a clean such as PHP can do, but can't find on Java alternative way as example:

 <?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;
?> 
Marin Sagovac
  • 3,932
  • 5
  • 23
  • 53
  • What's the practicality of writing a PHP class that is never changing and only does one thing? Do you have a more in-depth example that dynamically places code into PHP instead? For instance, if I wanted to add a function to the file - if that logic or approach were broken out into a method, it would be far easier and more readable than just putting long strings into Java. – Makoto Mar 03 '13 at 19:23
  • 1
    There are times I would love to have this functionality. Mostly when I'm doing testing and would rather not deal with file I/O just to test a simple parser. – Captain Ford Mar 03 '13 at 20:11
  • possible duplicate of [Java multiline string](http://stackoverflow.com/questions/878573/java-multiline-string) for the multiline, http://stackoverflow.com/questions/3034186/in-java-is-there-a-way-to-write-a-string-literal-without-having-to-escape-quote for not escaping quotes – Ciro Santilli OurBigBook.com Mar 20 '15 at 16:34
  • And explicit heredoc request with multiline and quote escape: http://stackoverflow.com/questions/2678483/simple-direct-heredoc-way-of-constructing-a-html-string-in-java – Ciro Santilli OurBigBook.com Mar 20 '15 at 16:42
  • this is finally possible with Java 13 text blocks, see here: https://openjdk.java.net/jeps/355 – Torben Knerr Dec 04 '19 at 11:11
  • @TorbenKnerr The java 13 `text blocks` are a step forward but also certainly not the same as heredocs: they do not support unfiltered strings or interpolation – WestCoastProjects Jan 06 '20 at 01:26

1 Answers1

16

Is there a heredoc equivalent in Java?

No, there is no direct heredoc alternative in the Java programming language.

Check this similar question.

Community
  • 1
  • 1