java输出utf8,在java中输出为UTF-8编码

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值