在java中输出为UTF-8编码 [英] output as UTF-8 encoding in java

查看:531
本文介绍了在java中输出为UTF-8编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用eclipse将程序中的输出文件出现问题.i将我的eclipse设置为UTF-8,并使用

  System.getProperty(file.encoding)

我得到UTF-8.i运行我的程序通过eclipse运行选项和输出(文本文件)以UTF-8编码。但是当我将源代码压缩到一个jar文件中时,输出文件会在某些字母表中显示错误,如Ã.what就是这个diff当在eclipse和frm jar文件中删除prog时,我必须在我的源代码中指定要在utf-8中编码的输出?pls help。



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



@dacwe:我尝试将源代码更改为

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

但是输出仍然没有正确编码。我想念这些东西?


@ div class =h2_lin>解决方案

在@Dave G的回答中进行了一些主要讨论后,



使用 java -Dfile.encoding = UTF-8 -jar your-jar-file.jar



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




  • 您是否真的重新包装了您的罐子?

  • 您是否致电 close () bout ? (例如,可能您的文件未更新)






这是一个完整的例子可能会让你走:

  public static void main(String ... args)throws Exception {
PrintWriter out =新的PrintWriter(新文件(hello.txt),UTF-8);
out.print(用utf-8写);
out.close();
}


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

 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.

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 :i tried changing my source code into

 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?

解决方案

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

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

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

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

这篇关于在java中输出为UTF-8编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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