繁体   English   中英

尝试包含Urdu字符串时为什么会出现编译错误?

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

我正在尝试添加urdu字符串غزل ,如下所示:

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();
     }
 }
}

当我尝试编译上述程序时,出现此错误。

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

我不明白这个错误。 为什么我得到这个,如何克服这个错误?

字节顺序标记

The byte order mark (BOM) is a Unicode character used to signal the endianness
(byte order) of a text file or stream. Its code point is U+FEFF. BOM use is
optional, and, if used, should appear at the start of the text stream. 
Beyond its specific use as a byte-order indicator, the BOM character may also 
indicate which of the several Unicode representations the text is encoded in.

因此,您需要剥离BOM表或将源文件转换为另一种编码。 Notepad ++可以转换单个文件的编码,因此我不知道Windows平台上的批处理实用程序。

文件开头的字符来自字节顺序标记,某些文本编辑器喜欢将其插入文件开头。 但是,Java编译器不接受带有BOM的文件。 您有两种选择:

  1. 使用文本编辑器,该文本编辑器允许以Unicode格式保存文件而无需 BOM,例如Notepad ++。
  2. 在源代码中仅使用ASCII字符。 在需要Unicode字符的地方,请使用\\uXXXX uXXXX-转义码。 JDK带有一个实用程序,可将“本机”文本转换为这种编码,称为native2ascii 例如,

     writer.write("غزل"); 

    将被转换成

     writer.write("\غ\ز\ل"); 

这取决于您的文本编辑器(在其中编辑Java源文件)使用的字符集。 尝试将其设置为UTF-8格式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM