简体   繁体   中英

How to add a new line when I print a text from a textarea to a file?

When I print a text that is in a JTextArea in a file it prints it in the same line . How can I add a new line in the file each time I have a new line in the JTextArea ? This is my code .

 print.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                JFileChooser fileChooser = new JFileChooser();
                if(fileChooser.showOpenDialog(null)== JFileChooser.APPROVE_OPTION){
                    File file = fileChooser.getSelectedFile();
                    try {

                        FileWriter fw = new FileWriter(file.getAbsoluteFile());
                        BufferedWriter bw = new BufferedWriter(fw);


                        bw.write(valid.getText()  );
                        bw.close();


                    } catch (IOException e1) {

                        e1.printStackTrace();
                    }                            
                }

                    }                                                    

            });

Simply use this function from your BufferedWriter object:

bw.newLine();

This command isn't the same like: <br> or \\n

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM