2

We have modified the application to support Non-Latin characters (Chinese & Thai) but we faced some issues in writing to CSV and resolved using below approach.

String line = "שלום, hello, привет";
    OutputStream os = new FileOutputStream("c:/temp/j.csv");
    os.write(239);
    os.write(187);
    os.write(191);

    PrintWriter w = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));

    w.print(line);
    w.flush();
    w.close();

The above one works but we are facing some other issues listed below.

  1. We opened a CSV file using below code and it was working fine.

    File file = new File(System.getProperty(TEMP_DIR_SYSPROP), filename);
    
    FileUtils.writeByteArrayToFile(file, bytes);
    
    Desktop.getDesktop().open(file);
    

But when it is written with UTF-8 we are facing below issue with excel.

NonLatin.csv is locked for editing by another user. Open 'Read-Only' or click 'Notify' to open and receive notification when the document is no longer in use. Or Cancel out.

  1. Double quotes is getting for few entries in csv which shows as different content when filter is applied.

Please suggest if there is any limitation with above approach (UTF-8) or is there any work around to handle this.

DanielBarbarian
  • 5,093
  • 12
  • 35
  • 44
Vijay
  • 21
  • 1
  • If you write with UTF-8 from Java, then it should cover any character you might have (I think). My feeling is that might be more of an Excel problem, so I added that tag to your question. – Tim Biegeleisen Aug 21 '18 at 05:55
  • Perhaps Apache FileUtils is not closing the file properly. Why are you using this third-party library at all? You can [use the standard Java library](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#write-java.nio.file.Path-byte:A-java.nio.file.OpenOption...-) instead. Also, did you write the Byte Order Mark into the second file just as you did with the first? – DodgyCodeException Aug 21 '18 at 06:33
  • I am using java.awt.Desktop only to open csv file. But i am getting locked by another user message when i write using UTF-8. FileUtils is the older version which is working fine. – Vijay Aug 21 '18 at 07:49

1 Answers1

0

Since you are dealing with CSV, I would look at how Excel is reading the file as. With Libre Office it is available as soon as I try to import the CSV file, so look for something similar in Excel enter image description here