aspose file tools*
The moose likes Java in General and the fly likes Export the multilanguage (Estonian , Lithunain, Latvian)data to CSV file using Outputstreamwriter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Export the multilanguage (Estonian , Lithunain, Latvian)data to CSV file using Outputstreamwriter" Watch "Export the multilanguage (Estonian , Lithunain, Latvian)data to CSV file using Outputstreamwriter" New topic
Author

Export the multilanguage (Estonian , Lithunain, Latvian)data to CSV file using Outputstreamwriter

vinay babu reddy
Greenhorn

Joined: May 25, 2010
Posts: 6
Hi all,

I am currently facing one issue with Export the data to csv, please help me out or give me idea or suggestion to solve the same

The problem here it is :

we are newly introducing 4 more new languages(Estonian, lithunaina, Latvian, polish) to existing application, i have a list screen where i am export the same list screen data to CSV(comma seperated format) file. The data export correctly as shows in List screen but when i opened the csv - the header labels (names) displayed with '?' marks. I have tried some of possible ways to solve the same, but i have no luck to crack this issue. I have tried to given as UTF-8 encoding for java.io.Outputstreamwrite class, even then my problem has nt solved.

Please post me any solution if you have.



Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42641
    
  65
Where does it show the question marks? Does that piece of software know that the file is in UTF-8? It may assume that it's in ASCII.


Ping & DNS - my free Android networking tools app
vinay babu reddy
Greenhorn

Joined: May 25, 2010
Posts: 6
Thanks for you reply

It shows the '?' marks in CSV file

Here is the code i made :

OutputStreamWriter writer = new OutstreamWriter(out,"UTF-8")

StrinBuffer sb= new StringBuffer();

sb.append(str).append(Quote).append(SEP)

writer.write(sb);

Please let me know any idea, is CSv will support all these languages
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42641
    
  65
What I meant was "what piece of software are you using to look at the CSV file?" Excel? OpenOffice? A console window?
vinay babu reddy
Greenhorn

Joined: May 25, 2010
Posts: 6
Thanks for your reply

Microsoft Excel 2007
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42641
    
  65
Excel apparently has problems with UTF-8 CSV files; try UTF-16 instead. You may wish to provide a list of various encodings for the user to choose from, like Cp1252, ISO-8859, UTF-8, UTF-16 etc.
vinay babu reddy
Greenhorn

Joined: May 25, 2010
Posts: 6
The same output with '?' marks.. do you have any example code to resolve.


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42641
    
  65
It has nothing to do with code; you need to look into how to make Excel work with different encodings in CSV files. A web search finds numerous hits for that kind of problem.
vinay babu reddy
Greenhorn

Joined: May 25, 2010
Posts: 6

Hi.. Thanks for all quick responses...
Finally we find out the solution ... here it is..

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Properties;
import java.io.*;


public class WriteToFile {
public static void main(String[] args) throws Exception {
Properties p = new Properties();
FileInputStream fr = new FileInputStream("D:\\EclipseWorkSpace\\Workspace\\Test\\src\\test\\acqtransaction\\package_lv_unicode.properties");
p.load(fr);
FileOutputStream fos = new FileOutputStream("c:\\out.csv");

byte[] bom = new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF }; // BOM values
fos.write(bom); // adds BOM

Writer out = new BufferedWriter(new OutputStreamWriter(fos, "UTF8"));
for (Object key : p.keySet()) {
Object val = p.get(key);
out.write("\"" + key + "\",\"" + val + "\"\n"); // adds key and value
}
out.close();
}

}

What is missing is the BOM (For example look at: http://en.wikipedia.org/wiki/UTF-8 look for BOM). If you open the CSV file you made for example in Notepad ++ or Notepad you will see that the special chars shows fine. But because of the missing BOM Excel can’t open the CSV file correct.

You should be able to fix the CSV problem with this example. Remember it’s just an example, the BOM part is the essential.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Please UseCodeTags next time.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Export the multilanguage (Estonian , Lithunain, Latvian)data to CSV file using Outputstreamwriter