java ascii utf8_Encoding - Convert US-ASCII to UTF-8?

I'm in America, near San Francisco, on a Mac, running garden variety Java 8. When I run your code, as is, I get the umlaut:

UTF-8

standü.png

I kinda figured I'd get this, because I know from past experience that my default Charset is some kind of unicode that will preserve foreign characters. Why is your default Charset ASCII? That seems to be your problem.

As I said in my comment, I think you can change "Charset.defaultCharset()" to "Charset.forName("UTF-8") to fix your problem. I can't be sure, but if I set it to "Charset.forName("ASCII"), then I get the output you do. So it seems this should fix things for you.

So just change:

String newstr = new String(f.getName().getBytes(Charset.defaultCharset()),"UTF-8");

to:

String newstr = new String(f.getName().getBytes(Charset.forName("UTF-8"),"UTF-8");

Also, won't this give you your umlaut:

String newstr = f.getName();

System.out.println(newstr);

as everything stays unicode all the way along, if I understand correctly. The problem comes in when you have to convert the name to a byte stream. If you don't have to do that, you'll be fine. If you do, then use UTF-8 explicitly instead of the default charset and I think you'll be good too.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值