2013-03-03 88 views
13

對於JAVA開發,我需要寫一個字符串爲「\ r \ t \ n <>」的文件,因爲從Java我想寫一個PHP文件。如果你不明白看看這個例子:在Java中有heredoc替代(heredoc as 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; 
?> 
+0

編寫一個永遠不會改變的PHP類的實用性只有一件事?您是否有更深入的示例將代碼動態地放入PHP中?例如,如果我想爲該文件添加一個函數 - 如果將該邏輯或方法分解爲一個方法,那麼與將長字符串放入Java相比,它將更容易且更具可讀性。 – Makoto 2013-03-03 19:23:16

+0

有些時候我很想擁有這個功能。大多數情況下,當我進行測試時,寧願不處理文件I/O來測試簡單的解析器。 – 2013-03-03 20:11:49

+0

多行的[Java多行字符串](http://stackoverflow.com/questions/878573/java-multiline-string)的可能的重複,http://stackoverflow.com/questions/3034186/in-java-is-有一個方法來寫一個字符串文字無需逃避報價不轉義報價 – 2015-03-20 16:34:26

回答

13

在Java中是否存在heredoc相等?

不,在Java編程語言中沒有直接heredoc替代方案。

與此相似的question