Why do I get a compilation error when I try to include an Urdu string?

1.5k Views Asked by At

I am trying to add an urdu string غزل as shown below:

class UnicodeCheck {
  public static void main(String args[]) {
   try {
    File f = new File("C:/Users/user/Desktop/unicodecheck.txt");
    FileWriter writer = new FileWriter(f);
    writer.write("غزل");
    writer.close();
   } catch(Exception exc) {
       exc.printStackTrace();
     }
 }
}

When I try to compile the above program I get this error.

UnicodeCheck.java:1: illegal character: \187
class UnicodeCheck {
 ^
UnicodeCheck.java:1: illegal character: \191
class UnicodeCheck {
  ^
2 errors

I do not understand this error. Why do I get this and how can I get over this error?

2

There are 2 best solutions below

0
On BEST ANSWER

The characters in the beginning of the file come from the the Byte Order Mark that some text editors like to insert into the beginning of a file. The Java compiler however does not accept files with BOM. You have two options:

  1. Use a text editor that allows saving files in Unicode without BOM, such as Notepad++.
  2. Use only ASCII characters in source code. Where you need Unicode characters use \uXXXX-escape codes. The JDK comes with a utility program to convert "native" text into this encoding, called native2ascii. For example,

    writer.write("غزل");
    

    would be converted into

    writer.write("\u063a\u0632\u0644");
    
1
On

It depends on the charset used by your Text editor (where you edit the java source file). Try to set it in UTF-8 format.