0

Possible Duplicate:
Writing multiline JTextArea content into file

i have written code that save file from my text area the problem is that it saves it all on a single line in the new text file and not as in my text box.below are my codes

    String text = dna_ta.getText();

    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory( new File( "./") );
    int actionDialog = chooser.showSaveDialog(this);
    if (actionDialog == JFileChooser.APPROVE_OPTION)
    {
        BufferedWriter out = null;
        try {
            File fileName = new File(chooser.getSelectedFile( ) + "" );
            if(fileName == null)
                return;
            if(fileName.exists())
            {
                actionDialog = JOptionPane.showConfirmDialog(this,
                                   "Replace existing file?");
                if (actionDialog == JOptionPane.NO_OPTION)
                    return;
            }
            out = new BufferedWriter(new FileWriter(fileName));
            out.write(text);
            out.close();

in my text area is it as follows

 asd
 aaaaaa

but in my save textfile it is as follows

 asdaaaaaa

i cant figure out where i have gone wrong to save it as same format as in my textarea.thank you

Community
  • 1
  • 1

1 Answers1

4

You could just use the JTextArea's write(...) method to do this for you:

out = new BufferedWriter(new FileWriter(fileName));
dna_ta.write(out);
out.close(); // after first checking if null

All components that derive from JTextComponent, including JTextArea, have this method which will write new lines using OS specific new line characters.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373