如何把TextArea中的换行符写入到文件中?求大神!
怎么把TextArea中的换行符写入到文件中?求大神!!!!问题是这样的:我在写个记事本,然后哪个内容面板是用Text
怎么把TextArea中的换行符写入到文件中?求大神!!!!
问题是这样的:
我在写个记事本,然后哪个内容面板是用TextArea做的,那么当我写好了要保存文件的时候直接通过.getText()获取到字符串,然后将字符直接通过FileOutputStream.write()函数写入到文件中,但是我查看文件的时候发现里面的内容根本就没有换行。。。。
求大神帮忙,怎么把换行符写入啊??
[解决办法]
你自己读取一下看看textarea里面的换行符是什么字符,然后替换一下,文件里的换行符是"\r\n"
[解决办法]
String temp = text1.getText();
text2.setText(temp);
File f = new File("d:/test/TextToTxt.txt");
try
{
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f)));
bw.write(temp);
bw.close();
}
catch (FileNotFoundException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (IOException e2)
{
// TODO Auto-generated catch block
e2.printStackTrace();
}
也是TextArea,这样操作可以换行。
[解决办法]查看字符的ascii,不是要你输出到控制台啊。将字符转换成int看。
------解决方案--------------------
File file=new File(file1.getAbsolutePath()+"\"+day+miu+".txt");
BufferedWriter bos=null;
try {
bos=new BufferedWriter(
new FileWriter(file));
String result=tfa.jta.getText();
String[] temp=result.split("[\\r\\n]");
//把数据写到文件中
for (int i = 0; i < temp.length; i++) {
bos.write(temp[i]);
bos.write("\r\n");
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} finally {
if(bos!=null){
try {
bos.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
关键String result=tfa.jta.getText();
String[] temp=result.split("[\\r\\n]");
我以前写的 测试过OK