简体   繁体   English

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

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

I am trying to add an urdu string غزل as shown below: 我正在尝试添加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();
     }
 }
}

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? 为什么我得到这个,如何克服这个错误?

Byte order mark 字节顺序标记

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.

So You need to strip the BOM or convert your source file to another encoding. 因此,您需要剥离BOM表或将源文件转换为另一种编码。 Notepad++ can convert a single files encoding, I'm not aware of a batch utility on the Windows platform for this. Notepad ++可以转换单个文件的编码,因此我不知道Windows平台上的批处理实用程序。

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. 但是,Java编译器不接受带有BOM的文件。 You have two options: 您有两种选择:

  1. Use a text editor that allows saving files in Unicode without BOM, such as Notepad++. 使用文本编辑器,该文本编辑器允许以Unicode格式保存文件而无需 BOM,例如Notepad ++。
  2. Use only ASCII characters in source code. 在源代码中仅使用ASCII字符。 Where you need Unicode characters use \\uXXXX -escape codes. 在需要Unicode字符的地方,请使用\\uXXXX uXXXX-转义码。 The JDK comes with a utility program to convert "native" text into this encoding, called native2ascii . JDK带有一个实用程序,可将“本机”文本转换为这种编码,称为native2ascii For example, 例如,

     writer.write("غزل"); 

    would be converted into 将被转换成

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

It depends on the charset used by your Text editor (where you edit the java source file). 这取决于您的文本编辑器(在其中编辑Java源文件)使用的字符集。 Try to set it in UTF-8 format. 尝试将其设置为UTF-8格式。

暂无
暂无

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

相关问题 当我尝试使用具有相同名称和参数类型的两个方法时,为什么会出现编译错误? - Why do I get a compilation error when I try to have two methods with the same name and parameter type? 为什么在使用[]运算符时出现编译错误? - Why do I get a compilation error when using the [] operator? 为什么在增强for循环后返回i时为什么会出现编译错误? - Why do I get a compilation error when returning i after enhanced for loop? 尝试将特定的字符串解析为int时,为什么会出现numberformatexception? - Why do I get a numberformatexception when I try to parse this specific string to an int? 为什么当我尝试从字符串中删除不间断空格时,我没有得到预期的结果? - Why when I try to remove non-breaking space from the string, I do not get the expected result? 当我尝试从Jframe中的表中获取字符串时,为什么会出现错误? - Why am I getting an error when I try to get a string from my table in a Jframe? 为什么在创建 JComboBox 对象时会出现此编译错误? - Why do i get this compilation error in creating JComboBox object? 为什么会出现“无法访问的代码”和“变量未初始化”的编译错误? - Why do I get an “unreachable code” and “variable not initialized” compilation error? 当我尝试在库存外部/没有物品的库存中单击时,为什么会出现错误? - Why do I get an error when I try to click outside the inventory / in the inventory where there's no item 为什么在尝试输出数组时会出现异常错误? - Why do I get an exception error when I try output an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM