1

My Java code produce csv file. when I open this CSV file as Notepad, I can not seen new line. I see all data just one line. Because of this reason , I can not add this file SQL database.

How can I solve this problem? For example, test.csv file :

TerminalSerialNo    IMEI    Site-Ref
77EC0067C8A1BA6462FCA25F8B1DFBE289F6A3EE    null    SIT-3
01B406234550B1A688000050BF7A60E2    null    SIT-3
19B32E9399CB51FACF858E126F9C7684578A8BAC    null    SIT-7

When I open this file as Notepad:

TerminalSerialNo,IMEI,Site-Ref,Site-Name77EC0067C8A1BA6462FCA25F8B1DFBE289F6A3EE,null,SIT3,01B406234550B1A688000050BF7A60E2,null,SIT-3,19B32E9399CB51FACF858E126F9C7684578A8BAC,null,SIT-7

What can I do to see data in Notepad with new line as CSV file?

Best Regards

  • 1
    try other text editors like notepad++ and vs-code – nima moradi Mar 07 '18 at 14:51
  • It's hard to tell without seeing any code, but my wild guess is you're saving file with Unix-style newlines (\n) and you're opening the file on Windows and Notepad expects Windows-style newlines (\r\n). You can check this using some modern text editor like Notepad++. – TheJavaGuy-Ivan Milosavljević Mar 07 '18 at 14:52
  • I know, when notepad ++ it seems ok. But I want to see like this in notepad. But the problem I want to solve in this notepad. If I solve , I easily add this file in SQL. Otherwise, SQL give me error while uploading data. –  Mar 07 '18 at 14:56
  • `FileWriter writer = new FileWriter("output.csv"); // headers writer.write("TerminalSerialNo,IMEI,Site-Ref,Site-Name\n"); writer.flush(); // data for(String[] arr: list) { String appender = ""; for(String s : arr){ writer.write(appender + s); appender = ","; } writer.write("\n"); writer.flush(); } writer.close();` –  Mar 07 '18 at 14:57
  • Did you try using `\r\n`? – XMB5 Mar 07 '18 at 15:10
  • thanks \r\n worked. thanks for your support! –  Mar 07 '18 at 15:20
  • A quick google search would have gotten you this answer on the Super User SE: https://superuser.com/questions/362087/notepad-ignoring-linebreaks Credit to Paul for the answer there. – Scrambo Mar 07 '18 at 14:54
  • I have also seen this answer. But this is not really solution. He says copy word then copy on notepad it will work. What do you think this is solution? I know this way work, as I mentioned I used this file in SQL database. Every time I copy this word file then copy again Notepad . this seems unprofessional way. –  Mar 07 '18 at 15:15

3 Answers3

0

Only in notepad?

Does your Java code use a line break?

\n
sp00n
  • 31
  • 8
  • Yes only in Notepad, I added my Java code. and I also uses new line \n. –  Mar 07 '18 at 15:03
0

use append("\n") and don't forget to close the writer.

Mujahid Masood
  • 111
  • 2
  • 8
0

Yes, I face this problem only notepad.I use "\n" and My Java code like this : ` FileWriter writer = new FileWriter("output.csv");

    // headers
    writer.write("TerminalSerialNo,IMEI,Site-Ref,Site-Name\n");
    writer.flush();

    // data
    for(String[] arr: list) {
        String appender = "";
        for(String s : arr){
            writer.write(appender + s);
            appender = ",";

        }
        writer.write("\n");
        writer.flush();
    }
    writer.close();


    `