-2

à¤à¥ विनियमन à¤à¤§à¤¿à¤¨à¤¿à¤¯à¤®

This output I obtain when I translated an English sentence. Is there any way to make it readable form ??

The Goal is to translate English Sentence to Hindi. The Hindi translated output is correctly obtained in the console. I need to write it to text file.

The translated sentence is set to "translation" and by getParameter() it is tried to save in to the file.

String translation = request.getParameter("translation"); OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(fileDir,true), "UTF-8"); BufferedWriter fbw = new BufferedWriter(writer);
fbw.write(translation);

Output file

Output file 1

swedha B
  • 3
  • 2

1 Answers1

0

This is an issue with mismatching character encoding (like UTF-8).
Make sure the character encoding of data that is returned from the request parameter is in UTF-8 encoding.
If the data is in a different encoding, you will have to use that encoding while writing to the file.

Syam Kumar S
  • 832
  • 2
  • 8
  • 26
  • You can try using ISO-8859-1. I guess you are getting the data from an http service. ISO-8859-1 is the default charset for http. – Syam Kumar S Feb 10 '17 at 08:20
  • I checked the character encoding of the data returned by using String val=request.getCharacterEncoding(); System.out.println(val); and it returns "null". Also I checked using ISO-8859-1 but no change in the output. – swedha B Feb 10 '17 at 10:31
  • It obviously won't be ISO-8859-1, as that charset doesn't include the Devanagari characters. It would have to be UTF-8 or UTF-16, if this is actually the cause of the problem. – Dawood ibn Kareem Feb 10 '17 at 10:59
  • In which console you are seeing the correct output? How are you printing it? – Syam Kumar S Feb 10 '17 at 11:21
  • In the eclipse's console, it is printing the output correctly. Simply using System.out.println(); as I have given the pageEncoding and CharacterEncoding as UTF-8 in the beginning of the tags I think its printing properly. – swedha B Feb 10 '17 at 11:39
  • If it is working like that, I don't think there is an issue with the file. Can u upload the generated file ? Which editor are you using to view the file? – Syam Kumar S Feb 10 '17 at 13:17
  • Please upload it as a *.txt* file, so that i can verify it's contents. – Syam Kumar S Feb 12 '17 at 18:09
  • Thanks a lot...You asked me to upload the file na..So I thought of how to upload here.Then I thought I will share the link after uploading to google drive. But suddenly I just opened after uploading using "Open with google docs"....To my surprise, I able to view the translated output. Then I choose the libre office 5 and I was able to view the content. As you mentioned earlier this might be a problem with my editor that I am choosing for viewing..... Anyways thanks because, If you don't ask for uploading I may not have viewed this surprise...Thanks again :) – swedha B Feb 13 '17 at 06:21
  • So we can conclude that there is no issue with the code. And the issue is that your text editor is not supporting Hindi characters. Please feel free to upvote or mark the answer as correct is you feel so. :) – Syam Kumar S Feb 13 '17 at 07:06
  • But why I am able to view output file properly and not the output1 file with the same libre office 5?? – swedha B Feb 13 '17 at 07:26
  • I think output1 is having corrupted data. Can you tell me the differences in both the files and the way you generated them? – Syam Kumar S Feb 14 '17 at 10:27
  • Problem solved ....:) There was an error in my code actually...http://stackoverflow.com/questions/1365806/why-the-character-is-corrupted-when-use-request-getparameter-in-java I referred this link – swedha B Feb 14 '17 at 11:19