gpt4 book ai didi

java - 将多行字符串写入文件

转载 作者:行者123 更新时间:2023-12-02 00:06:25 27 4
gpt4 key购买 nike

我正在使用 bufferedWriter 写入这样的文件

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

/**
*
* @author Jesus With Issues
*/
public class thewrite {

/**
* Prints some data to a file using a BufferedWriter
*/
public void writeToFile(String filename) {

BufferedWriter bufferedWriter = null;

try {

//Construct the BufferedWriter object
bufferedWriter = new BufferedWriter(new FileWriter(filename));

//Start writing to the output stream
bufferedWriter.write("First Line to be written");
bufferedWriter.newLine();
bufferedWriter.write("Second Line to be written");

} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
//Close the BufferedWriter
try {
if (bufferedWriter != null) {
bufferedWriter.flush();
bufferedWriter.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new thewrite().writeToFile("filed.txt");
}
}

但是,我想要一个间距良好的字符串,而不是一行文本

<article class="hello">
<section>
<header>
<h1>Markdown notes</h1>
<p>Lorem ipsum</p>
</header>
<footer>
<h4>Citations</h4>
<p>Loremed ipsumed</p>
</footer>
</section>
</article>

我想写入上面的字符串,一旦写入完成,我想要一个新行。bufferedWritter 中有一个现成的函数可以做到这一点吗?

最佳答案

当您需要换行时,可以使用换行序列。

  write("This is a line\nAnd this is a second line");

\n 将创建一个新行。

关于java - 将多行字符串写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13776587/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com