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?
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:
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, callednative2ascii
. For example,would be converted into