i wrote a code to create a csv file in java and it works fine for english characters but not for chinese characters. How can i Solve this. i tried modifying it but then it does not show comma separated values when opened in text file. please reply.

package com.hp.col.smartquote.web.action;


import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hp.col.smartquote.dao.DistributorResellerMapDAO;
import com.hp.col.smartquote.model.DistributorResellerMap;
import com.hp.col.smartquote.model.User;
import com.hp.col.smartquote.service.translation.TranslationManager;
import com.hp.col.smartquote.util.IOUtils;
import com.hp.col.smartquote.util.LocalizationManager;
import com.hp.col.smartquote.web.util.NullForward;
import com.hp.col.smartquote.web.util.WebUtils;


public class ViewDistributorResellerMapping extends BaseAction{
	
	/*private static final String DISTRESS_CSV = "DISTRESS_CSV";
	private static final String CSV_LIST_SEPARATOR = ", ";*/
	
	
public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

	      User user = LocalizationManager.getUser();
	      ServletOutputStream fileOut = response.getOutputStream();
			
          List distResellerMapLst = new ArrayList(); 
          distResellerMapLst = DistributorResellerMapDAO.getInstance().getValueFromDistReseMap(user);
			StringBuffer sb = exportResult(distResellerMapLst,request);
			
			String encoding = "UTF-16LE";
						
			WebUtils.prepareStreamedResponse(response, "application/x-download;charset="+ encoding , "quoteList.csv", sb.length(), true);
			
			
			//SQ LTP 1A changes done by Aswani for distributor reseller mapping excel
			byte[] bytes = sb.toString().getBytes(encoding);
			bytes[0] = (byte)255;
			bytes[1] = (byte)254;
			ByteArrayInputStream bis = new ByteArrayInputStream(bytes);							
			IOUtils.copyStream(bis, response.getOutputStream());
		
			//fileOut.write(sb.toString().getBytes());
			//fileOut.flush();
			

			return NullForward.getInstance();

	
	}
	
	protected StringBuffer exportResult(List distResellerMapLst,HttpServletRequest req)
	{
		
		for (Iterator iter = distResellerMapLst.iterator(); iter.hasNext();)
		{
			DistributorResellerMap distResellerMap = (DistributorResellerMap) iter.next();
			
		}

	

		int size = distResellerMapLst.size();
		StringBuffer sb = new StringBuffer(Math.max(1, size) * 100);
		Locale loc = LocalizationManager.getLocale();
		
		
		//StringBuffer sb = new StringBuffer(); 
	
		sb.append(" ");
		sb.append(TranslationManager.getService().translateKey("common.countryCode", loc));
		sb.append("\t");
		sb.append(TranslationManager.getService().translateKey("quote.resellerPartnerProId", loc));
		sb.append("\t");
		sb.append(TranslationManager.getService().translateKey("quote.distributorPartnerProId", loc));
		sb.append("\t");
		sb.append(TranslationManager.getService().translateKey("quote.activeFlag", loc));
		sb.append("\t");
		sb.append("\n");

		DistributorResellerMap distResellerMap ;


		for (Iterator iter = distResellerMapLst.iterator(); iter.hasNext();)
		{
			distResellerMap = (DistributorResellerMap) iter.next();

			sb.append(distResellerMap.getCountryCode());
			sb.append("\t");
			sb.append(distResellerMap.getResellerPartnerProId());
			sb.append("\t");
			sb.append(distResellerMap.getDistributorPartnerProId());
			sb.append("\t");
			sb.append(distResellerMap.getActiveFlag());
			
			sb.append("\n");
			
		
		}

		return sb;
	}
	
}

you have to Encode your outPut/input Straem with proper Charset

byte input/outputSteam[] = myString.getBytes("ISO-XXXX-X"); 
String myEncodedString = new String(input/outputSteam, "ISO-XXXX-X");

//NOTE X == equals Numbers represents chinese Charset from Native OS

First of all, you would have to be able to generate Unicode and not ASCII code.
A *.csv file is a TEXT file, which uses ASCII code.
You cannot use Unicode in a text file and use a regular text editor to view.
You would need a special text editor that can handle Unicode.
(One may exists, but I've never heard of one.)
MAYBE MS Excel can import a *.csv file and convert the Unicode,
but you would have to try this.

@hfx642 I see that you are from native ACSII languages, Native OS and especially Win doesn't care about that and pretty ignore file saves in Unicode/UTF8/UTF16 in case that Native OS is Localized (but this file is backward compactible with any of programing language, but not readable in MsOffice ...),

(for for readable document in MsOffice) I must remove (convert) all non-ACSII chars to ACSII, or Encode (File, Stream, whatever user services from/to Native OS) with cp1250/ISO 8859-2, then is for Win OS pretty readable, but not with backwards compactible for PL without encode back to Unicode/UTF16 (majorities of todays PL is based on Unicode/UTF8/UTF16)

Edited 5 Years Ago by mKorbel: n/a

This article has been dead for over six months. Start a new discussion instead.