如何编译一个编码为“UTF-8”的java源文件? [英] How to compile a java source file which is encoded as "UTF-8"?

查看:329
本文介绍了如何编译一个编码为“UTF-8”的java源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我保存了我的Java源文件,指定它的编码类型为UTF-8(使用记事本,默认Notepad的编码类型是ANSI),然后我尝试使用:

I saved my Java source file specifying it's encoding type as UTF-8 (using Notepad, by default Notepad's encoding type is ANSI) and then I tried to compile it using:

javac -encoding "UTF-8" One.java

但发生错误讯息

One.java:1: illegal character: \65279

?public class One {

^
1 error

有没有其他方法,我可以编译这个?

Is there any other way, I can compile this?

这是源代码:

public class One {
    public static void main( String[] args ){
        System.out.println("HI");
    }
} 


推荐答案

被视为UTF-8,否则值为65279的字符永远不会出现。 javac 期望您的源代码在平台默认编码中,根据 javac 文档

Your file is being read as UTF-8, otherwise a character with value "65279" could never appear. javac expects your source code to be in the platform default encoding, according to the javac documentation:


如果未指定 -encoding ,则使用平台默认转换器。 / p>

If -encoding is not specified, the platform default converter is used.

十进制65279是十六进制FEFF,它是 Unicode字节顺序标记(BOM)。这在UTF-8中是不必要的,因为UTF-8总是被编码为一个八位字节流,并且没有字节顺序问题。

Decimal 65279 is hex FEFF, which is the Unicode Byte Order Mark (BOM). It's unnecessary in UTF-8, because UTF-8 is always encoded as an octet stream and doesn't have endianness issues.

记事本喜欢坚持在BOM中, 没有必要,但有些程序不喜欢找到它们。正如其他人所指出的,记事本不是一个很好的文本编辑器。切换到不同的文本编辑器几乎肯定会解决您的问题。

Notepad likes to stick in BOMs even when they're not necessary, but some programs don't like finding them. As others have pointed out, Notepad is not a very good text editor. Switching to a different text editor will almost certainly solve your problem.

这篇关于如何编译一个编码为“UTF-8”的java源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆