1

I have a SQL query which returns only one column with object Ids. After executing the query in management studio, When I right click on the result grid and select "Save Results As..." to CSV, output gets properly stored in CSV file.

Output result set

But when I tried reading the same output file from a java program, then I observed few special characters have got added at the beginning of the file (before first record).

So I typed the file in command prompt and I could see the special characters. enter image description here The characters are not visible in any of the editor (Notepad/Notepad++/EditPlus). That means the special characters are getting added to the output file while exporting the results.

If I select all the records and copy-paste them in Ms-Excel then there is no issue of special characters.

Has anybody faced this issue? Any way to avoid it?

A N
  • 394
  • 2
  • 8
  • 18
  • 1
    Do the Java methods you're using support Unicode? Just thinking it might be the byte order mark: http://stackoverflow.com/questions/16262691/xc8-error-224-illegal-directive-first-line/16273557#16273557 – PeterJ Jan 16 '15 at 06:03
  • 1
    Did you just "save" or "save with encoding"? I agree with @PeterJ that it looks like a byte order mark, which is used in UTF-16BE / UTF-16 in Java / BigEndianUnicode in .NET. – Solomon Rutzky Jan 16 '15 at 06:16

1 Answers1

0

Got it! Thanks @PeterJ and @srutzky for pointing the error correctly. I was using below code to read the file.

FileInputStream fstream = new FileInputStream(this.getProperty("input.file"));
br = new BufferedReader(new InputStreamReader(fstream));

To handle UTF coding, I modified the code as below and that worked:

FileInputStream fstream = new FileInputStream(this.getProperty("input.file"));
br = new BufferedReader(new InputStreamReader(fstream,"UTF8"));
A N
  • 394
  • 2
  • 8
  • 18