在java中输出为UTF-8编码。

时间:2023-01-06 21:46:28

I am having problem with the output file from a program using eclipse .i set my eclipse to UTF-8 and with

我使用eclipse的一个程序的输出文件存在问题,我将eclipse设置为UTF-8,并使用。

 System.getProperty("file.encoding") 

i get UTF-8.i ran my prog via eclipse run-option and the output (a text file) is encoded in UTF-8.but when i compressed the source code into a jar file,the output file shows error in some of the alphabet like Ã.what is with this diff when ruuning the prog in eclipse and frm jar file?and do i have to specify the output to be encoded in utf-8 in my source code?pls help.

我得到utf - 8。我通过eclipse run-option运行我的prog,输出(一个文本文件)用UTF-8编码。但是当我将源代码压缩到一个jar文件中时,输出文件会显示一些字母表中的错误。在eclipse和frm jar文件中ruuning prog的时候,这个diff是什么?我是否需要在我的源代码中指定用utf-8编码的输出?请帮助。

help from @dacwe indeed produced the desired output.but may i know how can i run my executable .jar file outside command line?how can the -Dfile.encoding=UTF-8

@dacwe的帮助确实产生了所需的输出。但是我可以知道如何运行我的可执行文件。jar文件在命令行外面吗?如何-Dfile.encoding = utf - 8

@dacwe :i tried changing my source code into

@dacwe:我试过把源代码修改成。

 BufferedWriter bout  = new java.io.BufferedWriter(new java.io.OutputStreamWriter(
                new java.io.FileOutputStream(filename), "UTF-8"));

but the output still is not encoded correctly.anything i miss here?

但是输出仍然没有正确编码。我想念这里吗?

2 个解决方案

#1


11  

After some major discussion in @Dave G's answer!

在对@Dave G的回答进行了一些主要讨论之后!

Using java -Dfile.encoding=UTF-8 -jar your-jar-file.jar works.

使用java -Dfile。utf - 8编码= jar jar文件。jar的作品。

Updating your code as @Dave G suggested (and your edit) should work.

更新你的代码如@Dave G建议(和你的编辑)应该工作。

  • Have you really repackaged your jar?
  • 你真的把你的罐子重新包装了吗?
  • Do you call close() on bout? (e.g. maybe your file isn't updated)
  • 你能不能打个招呼?(例如,你的档案可能没有更新)

Here is a full example that might get you going:

下面是一个完整的例子:

public static void main(String... args) throws Exception {
    PrintWriter out = new PrintWriter(new File("hello.txt"), "UTF-8");
    out.print("written in utf-8");
    out.close();
}

#2


1  

When you run from a JAR file are you setting the file.encoding property by -Dfile.encoding? If not, you can either

当您从一个JAR文件运行时,您正在设置文件。由-Dfile.encoding编码属性?如果没有,你也可以。

a) open the stream explicitly with that encoding. for this you will have to create an OutputStream and then wrap that in an OutputStreamWriter explicitly indicating the character encoding.

a)用该编码显式地打开流。为此,您必须创建一个OutputStream,然后将其打包在一个OutputStreamWriter中,显式地指示字符编码。

or

b) set the property as the first thing in your main method using System.setProperty("file.endcoding");

b)使用System.setProperty(“file.endcoding”)将属性设置为主要方法中的第一项。

note @dacwe pointed out something I forgot ... corrected my answer.

注意:@dacwe指出了一些我忘记的事情……纠正了我的答案。

#1


11  

After some major discussion in @Dave G's answer!

在对@Dave G的回答进行了一些主要讨论之后!

Using java -Dfile.encoding=UTF-8 -jar your-jar-file.jar works.

使用java -Dfile。utf - 8编码= jar jar文件。jar的作品。

Updating your code as @Dave G suggested (and your edit) should work.

更新你的代码如@Dave G建议(和你的编辑)应该工作。

  • Have you really repackaged your jar?
  • 你真的把你的罐子重新包装了吗?
  • Do you call close() on bout? (e.g. maybe your file isn't updated)
  • 你能不能打个招呼?(例如,你的档案可能没有更新)

Here is a full example that might get you going:

下面是一个完整的例子:

public static void main(String... args) throws Exception {
    PrintWriter out = new PrintWriter(new File("hello.txt"), "UTF-8");
    out.print("written in utf-8");
    out.close();
}

#2


1  

When you run from a JAR file are you setting the file.encoding property by -Dfile.encoding? If not, you can either

当您从一个JAR文件运行时,您正在设置文件。由-Dfile.encoding编码属性?如果没有,你也可以。

a) open the stream explicitly with that encoding. for this you will have to create an OutputStream and then wrap that in an OutputStreamWriter explicitly indicating the character encoding.

a)用该编码显式地打开流。为此,您必须创建一个OutputStream,然后将其打包在一个OutputStreamWriter中,显式地指示字符编码。

or

b) set the property as the first thing in your main method using System.setProperty("file.endcoding");

b)使用System.setProperty(“file.endcoding”)将属性设置为主要方法中的第一项。

note @dacwe pointed out something I forgot ... corrected my answer.

注意:@dacwe指出了一些我忘记的事情……纠正了我的答案。