问题是这样的:
我在写个记事本,然后哪个内容面板是用TextArea做的,那么当我写好了要保存文件的时候直接通过.getText()获取到字符串,然后将字符直接通过FileOutputStream.write()函数写入到文件中,但是我查看文件的时候发现里面的内容根本就没有换行。求大神帮忙,怎么把换行符写入啊??
我在写个记事本,然后哪个内容面板是用TextArea做的,那么当我写好了要保存文件的时候直接通过.getText()获取到字符串,然后将字符直接通过FileOutputStream.write()函数写入到文件中,但是我查看文件的时候发现里面的内容根本就没有换行。求大神帮忙,怎么把换行符写入啊??
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,这样操作可以换行。
这是我的样例,比较粗糙。在左侧TextArea中输入几行(通过Enter换行),写入d盘的文件,没问题的呀!要不LZ对比试试,看看有啥区别?
import java.awt.TextArea;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class StrInTextArea
extends JFrame
{ public StrInTextArea()
{
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 200, 500, 500); JPanel p = new JPanel();
final TextArea text1 = new TextArea(4, 20);
JButton button = new JButton("提交");
final TextArea text2 = new TextArea(4, 20); p.add(text1);
p.add(button);
p.add(text2);
p.setVisible(true);
add(p); button.addMouseListener(new MouseListener()
{ public void mouseClicked(MouseEvent e)
{
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();
}
} public void mouseEntered(MouseEvent e)
{
// TODO Auto-generated method stub } public void mouseExited(MouseEvent e)
{
// TODO Auto-generated method stub } public void mousePressed(MouseEvent e)
{
// TODO Auto-generated method stub } public void mouseReleased(MouseEvent e)
{
// TODO Auto-generated method stub } });
setVisible(true);
}
public static void main(String[] args)
{
new StrInTextArea();
}
}
这是我的样例,比较粗糙。在左侧TextArea中输入几行(通过Enter换行),写入d盘的文件,没问题的呀!要不LZ对比试试,看看有啥区别?
import java.awt.TextArea;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class StrInTextArea
extends JFrame
{ public StrInTextArea()
{
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 200, 500, 500); JPanel p = new JPanel();
final TextArea text1 = new TextArea(4, 20);
JButton button = new JButton("提交");
final TextArea text2 = new TextArea(4, 20); p.add(text1);
p.add(button);
p.add(text2);
p.setVisible(true);
add(p); button.addMouseListener(new MouseListener()
{ public void mouseClicked(MouseEvent e)
{
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();
}
} public void mouseEntered(MouseEvent e)
{
// TODO Auto-generated method stub } public void mouseExited(MouseEvent e)
{
// TODO Auto-generated method stub } public void mousePressed(MouseEvent e)
{
// TODO Auto-generated method stub } public void mouseReleased(MouseEvent e)
{
// TODO Auto-generated method stub } });
setVisible(true);
}
public static void main(String[] args)
{
new StrInTextArea();
}
}你那个是不行的,我这么写了:
int j = 0, i = 0;
char[] chars = data.toCharArray();
for (i = 0; i < chars.length; i++) {
if ((int) chars[i] == 10) {
bfw.write(chars, j, i - j);
bfw.write(WRAP_DELIMITER);
j = i;
bfw.flush();
}
}
bfw.write(chars, j, i - j);
bfw.flush();
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