如何将标准输出更改为"UTF-8"?在Java中 [英] How can I change the Standard Out to "UTF-8" in Java

查看:44
本文介绍了如何将标准输出更改为"UTF-8"?在Java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Java程序从网站上下载了文件,标题如下所示

I download a file from a website using a Java program and the header looks like below

Content-Disposition attachment;filename="Textkürzung.asc";

未指定编码

我要做的是下载后将文件名传递给另一个应用程序以进行进一步处理.我用

What I do is after downloading I pass the name of the file to another application for further processing. I use

System.out.println(filename);

在标准输出中,字符串打印为Textk³rzung.asc

In the standard out the string is printed as Textk³rzung.asc

如何在Java中将标准输出更改为"UTF-8"?

How can I change the Standard Out to "UTF-8" in Java?

我尝试将其编码为"UTF-8",但内容仍然相同

I tried to encode to "UTF-8" and the content is still the same

更新:

我能够在不进行任何代码更改的情况下解决此问题.在我从其他应用程序将此文件称为jar文件的地方,我执行了以下操作

I was able to fix this without any code change. In the place where I call this my jar file from the other application, i did the following

java -DFile.Encoding = UTF-8 -jar ....

这似乎已经解决了问题

谢谢大家的支持

推荐答案

您看到的结果表明您的控制台希望文本使用Windows代码页850"编码-字符ü具有Unicode代码点U + 00FC.字节值0xFC在Windows代码页850中呈现为³.因此,如果您希望该名称正确显示在控制台上,则需要使用"Cp850"编码进行打印:

The result you're seeing suggests your console expects text to be in Windows "code page 850" encoding - the character ü has Unicode code point U+00FC. The byte value 0xFC renders in Windows code page 850 as ³. So if you want the name to appear correctly on the console then you need to print it using the encoding "Cp850":

PrintWriter consoleOut = new PrintWriter(new OutputStreamWriter(System.out, "Cp850"));
consoleOut.println(filename);

这是否是您的其他应用程序"所期望的另一个问题-如果其他应用程序也正在读取其标准输入为Cp850,则该其他应用程序只会看到正确的名称.

Whether this is what your "other application" expects is a different question - the other app will only see the correct name if it is reading its standard input as Cp850 too.

这篇关于如何将标准输出更改为"UTF-8"?在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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