0

I have a java code that reads an excel file and writes it to a text file. When i get the output text file, I can see only symbols like this in the notepad. "‷搮ㄱ〱〰㄰㄰〠〰〠〰〠〰〠〰〠〰〠〰〠〰〠〰〠〰〠〰〠" But if i open the text file in wordpad or ms-word, the data is correctly displayed. What could be the error?

migrated from stackoverflow.com Apr 20 '10 at 13:17

This question came from our site for professional and enthusiast programmers.

  • What's the encoding of the text? – Matt Ball Apr 20 '10 at 13:11
3

The error lies in using Notepad. Notepad uses the first couple bytes of a file to decide what the encoding is, you have to open a file with a specific encoding to read it properly.

Here's a link to learn ya: http://blogs.msdn.com/oldnewthing/archive/2007/04/17/2158334.aspx

Lesson learned: Don't use notepad.

  • 4
    Try notepadd++ as a free notepad replacement (notepad-plus.sourceforge.net/uk/site.htm). Very handy and knows how to handle encoding properly. Also does syntax highlighting for various languages and XML. – Tom Cabanski Apr 20 '10 at 13:15
  • or... output the encoding preamble to the file – angry person Apr 20 '10 at 13:17
2

It's quite likely that the first part of your data looks superficially like Unicode in a certain encoding (UTF-16). This has been a problem in past versions of Windows and can be read about in this article. However, I think since Windows Vista that particular issue was fixed.

A simple workaround would be not to use Notepad for that data.

0

The command line switch: notepad /A <filename>, can be used to force a file to open as though it has the ANSI encoding, which in this case would cause the file to be opened correctly.

The problem here is the 'Bush hid the facts' bug, some txt files have a 2 or 3 byte sequence called a byte order mark (BOM) identifying the correct encoding for the file, if a file lacks this, Notepad guesses the file's encoding and may guess incorrectly. There is no BOM to indicate that a file is ANSI, and so ANSI files are always at risk of being guessed incorrectly. Some text editors assume that a file is ANSI if it has no BOM, and Notepad's approach to determining a file's encoding may differ depending on the version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy