问题是这样的:
我在写个记事本,然后哪个内容面板是用TextArea做的,那么当我写好了要保存文件的时候直接通过.getText()获取到字符串,然后将字符直接通过FileOutputStream.write()函数写入到文件中,但是我查看文件的时候发现里面的内容根本就没有换行。求大神帮忙,怎么把换行符写入啊??

解决方案 »

  1.   

    你自己读取一下看看textarea里面的换行符是什么字符,然后替换一下,文件里的换行符是"\r\n"
      

  2.   

    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,这样操作可以换行。
      

  3.   

    怎么打出来呢?我直接输出到控制台上,它是可以换行的。看不到它的换行符是什么查看字符的ascii,不是要你输出到控制台啊。将字符转换成int看。
      

  4.   

    (⊙o⊙)…  不可以的。
    这是我的样例,比较粗糙。在左侧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();
        }
    }
      

  5.   

    (⊙o⊙)…  不可以的。
    这是我的样例,比较粗糙。在左侧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();
      

  6.   

    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