我将多行作为来自 JTextarea 的输入, 如果我把它写在一个文件中,那么我会得到多行写在文件中的一行中
例子:
在 JTextArea 中:
I
am
a
student
表示:variable.text="我'\n'是'\n'a'\n'学生"; 当我在文件中写入字符串 s 时,我得到:
I am a student
但我希望该文件包含与我作为输入方式提供的内容相同的内容--->
I
am
a
student
这是写入文件的代码:
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(file), "UTF16"));
int size=1;
for(Tableclass variable:tablevector)
{
out.write(variable.Text);
out.newLine();
size++;
}
out.close();
最佳答案
稍微好一点的版本是:
try {
PrintWriter fstream = new PrintWriter(new FileWriter("log.txt"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for(String word : jTextAreaName.getText().split("\n")) {
fstream.println(word);
}
fstream.flush();
关于java - 当多行作为 JTextarea 的输入时,如何在文件中写入多行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10535395/