开发者

How to add newlines while writing to file using java?

    String filepath=jTextField1.getText();
    FileReader fr;
    try {            
        fr = new FileReader("C:\something.txt");
        BufferedReader br = new BufferedReader(fr);
        String sf="";
        while((br.readLine())!=null){
            sf=sf+br.readLine()+"\n";               
        }
        jTextArea1.setText(sf);

    } catch (Exception ex) {
        Logger.getLogger(DesktopApplication1View.class.getName()).log(Level.SEVERE, null, ex);
    }

The output file contain 开发者_JS百科read character write in single line the whole length. How to get file with newlines?


According to your code you are READING a file and then outputting the text to what looks like a swing text component. I don't see where it is being declared so I can't say for sure what kind of component it is. You need to set your Text component to be multiline.


Instead of the "\n", use System.getProperty("line.separator") to ensure that you use the right line separator for your operating system.

As Manoj suggested, use StringBuilder instead of concatenating a String.

And, as maple_shaft already suggested, you need to set your Swing Text component to accept a multi-line string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜