Java 将Jtext区域的精确文本写入文件
大家好,我的应用程序中有一个代码,可以将jtextarea上的数据保存到.txt文件中。 问题是,当我在jtextarea上键入多行文本并将其保存到“.txt”文件中时,整个数据将被写成一行文本Java 将Jtext区域的精确文本写入文件,java,swing,io,jtextarea,filewriter,Java,Swing,Io,Jtextarea,Filewriter,大家好,我的应用程序中有一个代码,可以将jtextarea上的数据保存到.txt文件中。 问题是,当我在jtextarea上键入多行文本并将其保存到“.txt”文件中时,整个数据将被写成一行文本 String content = txtDescriptionCity.getText(); //jtextarea File file = new File("C:\\Tour v0.1\\Descriptions\\City\\"+txtcityname.getText()+".txt");
String content = txtDescriptionCity.getText(); //jtextarea
File file = new File("C:\\Tour v0.1\\Descriptions\\City\\"+txtcityname.getText()+".txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
我想写的文字,因为它是在jtextarea。。。像这样
“文本第一行
第二行
文本第三行“您可以拆分然后写入,在我的代码中,我根据“.”(句号)拆分字符串
您可以拆分然后写入,在我的代码中,我根据“.”(句号)拆分字符串
您可以拆分然后写入,在我的代码中,我根据“.”(句号)拆分字符串
您可以拆分然后写入,在我的代码中,我根据“.”(句号)拆分字符串
如果输入的行是Enter,请执行以下操作:
content = content.replaceAll("\r?\n", "\r\n");
然后使用Windows下的实际换行符:\r\n
(CR+LF)。记事本中不显示单个\n
。不太可能
如果多行是由换行引起的,则情况更为困难
如果通过添加空格
content = content.replaceAll(" +", "\r\n");
如果输入的行是Enter,请执行以下操作:
content = content.replaceAll("\r?\n", "\r\n");
然后使用Windows下的实际换行符:\r\n
(CR+LF)。记事本中不显示单个\n
。不太可能
如果多行是由换行引起的,则情况更为困难
如果通过添加空格
content = content.replaceAll(" +", "\r\n");
如果输入的行是Enter,请执行以下操作:
content = content.replaceAll("\r?\n", "\r\n");
然后使用Windows下的实际换行符:\r\n
(CR+LF)。记事本中不显示单个\n
。不太可能
如果多行是由换行引起的,则情况更为困难
如果通过添加空格
content = content.replaceAll(" +", "\r\n");
如果输入的行是Enter,请执行以下操作:
content = content.replaceAll("\r?\n", "\r\n");
然后使用Windows下的实际换行符:\r\n
(CR+LF)。记事本中不显示单个\n
。不太可能
如果多行是由换行引起的,则情况更为困难
如果通过添加空格
content = content.replaceAll(" +", "\r\n");
首先,我们读取系统行分隔符,然后拆分内容字符串,并将新字符串数组的每个部分写入文件。
新文件编写器(*,true)代码>附加内容的标签
String seperator = System.lineSeparator();
String[] lines = content.split("\n");
FileWriter writer = new FileWriter(file ,true);
for (int i = 0 ; i < lines.length; i++){
writer.append(lines[i]+seperator);
}
writer.close();
}
String separator=System.lineSeparator();
字符串[]行=content.split(“\n”);
FileWriter=newfilewriter(file,true);
对于(int i=0;i
首先读取系统行分隔符,然后拆分内容字符串并将新字符串数组的每个部分写入文件。新文件编写器(*,true)代码>附加内容的标签
String seperator = System.lineSeparator();
String[] lines = content.split("\n");
FileWriter writer = new FileWriter(file ,true);
for (int i = 0 ; i < lines.length; i++){
writer.append(lines[i]+seperator);
}
writer.close();
}
String separator=System.lineSeparator();
字符串[]行=content.split(“\n”);
FileWriter=newfilewriter(file,true);
对于(int i=0;i
首先读取系统行分隔符,然后拆分内容字符串并将新字符串数组的每个部分写入文件。新文件编写器(*,true)代码>附加内容的标签
String seperator = System.lineSeparator();
String[] lines = content.split("\n");
FileWriter writer = new FileWriter(file ,true);
for (int i = 0 ; i < lines.length; i++){
writer.append(lines[i]+seperator);
}
writer.close();
}
String separator=System.lineSeparator();
字符串[]行=content.split(“\n”);
FileWriter=newfilewriter(file,true);
对于(int i=0;i
首先读取系统行分隔符,然后拆分内容字符串并将新字符串数组的每个部分写入文件。新文件编写器(*,true)代码>附加内容的标签
String seperator = System.lineSeparator();
String[] lines = content.split("\n");
FileWriter writer = new FileWriter(file ,true);
for (int i = 0 ; i < lines.length; i++){
writer.append(lines[i]+seperator);
}
writer.close();
}
String separator=System.lineSeparator();
字符串[]行=content.split(“\n”);
FileWriter=newfilewriter(file,true);
对于(int i=0;i
不要重新发明轮子。只需使用:
textArea.write(...);
它会将文档中的新行字符替换为您正在使用的平台的适当新行字符串。不要重新发明轮子。只需使用:
textArea.write(...);
它会将文档中的新行字符替换为您正在使用的平台的适当新行字符串。不要重新发明轮子。只需使用:
textArea.write(...);
它会将文档中的新行字符替换为您正在使用的平台的适当新行字符串。不要重新发明轮子。只需使用:
textArea.write(...);
它会将文档中的新行字符替换为您所使用平台的适当新行字符串。所有文本都将作为单个字符串,您需要拆分字符串,然后将其写入文本文件。所有文本都将作为单个字符串,您需要拆分字符串,然后将其写入文本文件。所有文本将以单个字符串的形式出现,您需要拆分字符串,然后将其写入文本文件。所有文本将以单个字符串的形式出现,您需要拆分字符串,然后将其写入文本文件。@user2136160,不要这么麻烦。只需使用所有Swing文本组件都支持的write(…)
方法即可。@user2136160,不要麻烦了。只需使用所有Swing文本组件都支持的write(…)
方法即可。@user2136160,不要麻烦了。只需使用所有Swing文本组件都支持的write(…)
方法即可。@user2136160,不要麻烦了。只需使用所有Swing文本组件支持的write(…)
方法即可。有趣,如javadoc+1中未记录,有趣,如javadoc+1中未记录,有趣,如javadoc+1中未记录,有趣,如javadoc+1中未记录,