difference between newLine() and carriage return(“\r”)

后端 未结 5 1906
失恋的感觉
失恋的感觉 2020-12-05 22:44

What is the difference between newLine() and carriage return (\"\\r\")? Which one is best to use?

File f = new File(strFileGenLoc);
BufferedWrit         


        
5条回答
  •  情深已故
    2020-12-05 23:22

    Assuming you mean this:

    public static String newLine = System.getProperty("line.separator");
    

    newLine is environment agnostic \r isn't.

    So newLine will give you \r\n on windows but \n on another environment.

    However, you shouldn't use this in a JTextArea and println will work fine with just \n on windows.

    Edit now that I've seen the code and your comment

    In your situation. I think you should use your own constant - \r\n

      File f = new File(strFileGenLoc);
      BufferedWriter bw = new BufferedWriter(new FileWriter(f, false));
      rs = stmt.executeQuery("select * from jpdata");
      while ( rs.next() ) {
        bw.write(rs.getString(1)==null? "":rs.getString(1));
        bw.write("\\r\\n");
      }
    

提交回复
热议问题