• Post Reply Bookmark Topic Watch Topic
  • New Topic

Write Arabic text into CSV file  RSS feed

 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write arabic text into CSV file but i get some junk characters in the file instead of arabic, while i try to export as txt file i can see the arabic charectars properly, if i create an xls using HSSFPOI i see the charectars correctly, only when i create CSV, the arabic text is not displaying properly:

Code:

PrintWriter pw = new PrintWriter("C:
WriteTest.CSV","UTF-8");
String text="متانتاتن";

pw.println("a csv file and now a new line is going to come");

pw.print(",");


pw.println(text); pw.print(",");

pw.println("حتاليبلات-حخهعغفق");

Output what i get in the CSV is:

a csv file and now a new line is going to come
متانتاتن
حتاليبلات-حخهعغفق

Please suggest..




 
Paul Clapham
Sheriff
Posts: 22521
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two steps in your process. You use your Java code to write the CSV (step 1) and then you use something else (you didn't say what) to look at it (step 2).

You're assuming the problem is in step 1. I think it's more likely that the problem is in step 2. Whatever that was.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... part of the reason being that CSV has no means to convey the character set it's stored in. So unless the software used in step 2 correctly guesses "UTF-8" (or the user tells it as much), and can display it, it's not going to work.
 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry I copied the wrong line from my code, actually i was trying to print in text file , to test whether the problem is in my code, anyways the text file could show the arabic charectars, where as the CSV could not show it, csv rather shows junk.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ruquia tabassum wrote:sorry I copied the wrong line from my code, actually i was trying to print in text file , to test whether the problem is in my code, anyways the text file could show the arabic charectars, where as the CSV could not show it, csv rather shows junk.


Since you have written the file using utf-8 you need to display it using a program that treats it as utf-8 and has glyphs for the characters you want to display. It has been asked before but you have not answered; what program are you using to display the csv file? If the answer is that you have listed the to the command line then that is most likely problem since on Windows that does not use utf-8 by default.
 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not reading the CSV file in any program. i am just taking the data from database and storing in csv file as a report. When I open manually it shows junk .
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ruquia tabassum wrote:When I open manually it shows junk .

That's what we're trying to find out from you: Which application are you using for opening it? Does it know that the file is encoded in UTF-8, and -more fundamentally- can it even handle UTF-8?
 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to open file manually, not using any program to open. my main concern is writing and viewing manually.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ruquia tabassum wrote:I need to open file manually, not using any program to open. my main concern is writing and viewing manually.


You are using a program to open the file. If you open with Notepad then you are using the Notepad program to view the XML content. If you open it with Excel then you are using the Excel program to view the XML content. If you use the 'type' command from the command prompt then you are using the 'type' program in conjunction with the Windows console.

So what program or programs are you using to display the file content?
 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Excel.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ruquia tabassum wrote:I am using Excel.


For the moment open the file with Notepad specifying the encoding as UTF-8 ( a drop-down list on the open dialogue) . You should then see the characters you expect.
 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, when I open the saved CSV with notepad, it shows me the arabic text properly. My requirement is to create CSV open in Excel.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ruquia tabassum wrote:Yes, when I open the saved CSV with notepad, it shows me the arabic text properly.

So the file is being written correctly.

My requirement is to create CSV open in Excel.

So as explained by everyone the problem has little or nothing to do with Java but pretty much everything to do with the program being used to display the content of the file.

My version of Excel is so old that it does not support UTF-8. I would expect that there was an option on your Excel to say what character encoding to use so spend a bit of time looking at the options that are available when opening the file with Excel.

I did find this http://www.herongyang.com/Unicode/Excel-Open-UTF-8-Text-File.html but I have no way of checking it against your Arabic .
 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the time. You mean any macro in Excel will do this?
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ruquia tabassum wrote:You mean any macro in Excel will do this?


I doubt if a macro is involved but I will have to leave this to others to handle since I don't have either the background or the technology needed.
 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Searching for "csv excel specify encoding" returned some highly relevant results.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!