-1

How can I read non alphanumeric characters (like Chinese characters) in Java? I tried to read a value from a .properties file but I get broken characters in output.

filename = some method returning the file in Chinese 

response.setProperty("Content-disposition", "attachment; filename=\"" + fileName +"\"");
response.setContentType("text/xlscharset=UTF-8");
response.setCharacterEncoding("utf-8");
Mureinik
  • 297,002
  • 52
  • 306
  • 350
JavaLearner
  • 389
  • 1
  • 3
  • 8

1 Answers1

0

Not sure, how are you reading file. I would suggest you to use BufferedReader to read file, which is as follows,

BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream("myFilePath"),"UTF8"));

Above piece of code will handle Chinese characters. Now, you car use br to process your property file. If your file is UTF-16, the replace UTF8 encoding with UTF-16 and it should do the job for you.

Abhishek
  • 6,912
  • 14
  • 59
  • 85
  • In my scenario the File Name itself contains chinese characters. Hence on using the name in the file path, it doesnot recognise the file. If i print the file name at backend, I get broken characters in console. – JavaLearner May 26 '15 at 04:56
  • You can get filename using the method given at http://stackoverflow.com/questions/1844688/read-all-files-in-a-folder and then pass the filename to `BufferedReader`. – Abhishek May 26 '15 at 04:57
  • My java string contains chinese characters. When I print that it shows broken character. I want my string to contain the chinese characters. Is that possible? – JavaLearner May 26 '15 at 05:46
  • @JavaLearner Yup, string can contain chinese characters. If you want to show on output screen/console then look at http://www.mkyong.com/java/how-to-display-chinese-character-in-eclipse-console/ for settings of eclipse.. – Abhishek May 26 '15 at 06:50
  • On downloading a file having Chinese Characters in its name, I am getting "File not found exception." But the file is present in the drive and the file name in the "Open and Save" popup is also fine. – JavaLearner May 26 '15 at 09:32
  • Did you tried getting files using the method shared in my last comment? – Abhishek May 26 '15 at 09:58
  • Yes. I gave the file path containing the file name(Chinese). BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fullPath),"UTF8")); OutputStream out = response.getPortletOutputStream(); byte buf[]=new byte[1024]; int len; while((len=inputStream.read(buf))>0){ ; out.write(buf,0,len); } – JavaLearner May 26 '15 at 10:05
  • You can use `br.readLine()` directly to get the content. You needn't use `buf[]` – Abhishek May 26 '15 at 10:53