java写文件ansi,如何将一个UTF字符串转换为ANSI和创建在SSD ANSI文本文件,JAVA,机器人...

I wrote a android app in java to get user answers and save them in a file.

The problem is that this file is saved in utf-8.

The end user will open these files in the IBM SPSS, an application for windows that can read files only in ANSI (windows-1252).

How do I create files in ANSI code to save in a ssd-card from java-android app?

I think I know that to convert Strings to ANSI I should use:

String unicode = new String(asciiBytes, "windows-1252");

Is that correct?

My code to save the file is this:

File interviewFile = new File(placeOfSDD, fileName);

FileWriter writer = new FileWriter(interviewFile, true);

writer.append(textBody);

writer.flush();

writer.close();

"textBody" is the String to convert to ANSI, and the "interviewFile" is the file to be saved as ANSI too.

Thanks for the help!!

解决方案

Strings are represented within the JVM as Unicode. When you write it out, you need to convert it to an appropriate character set.

To do this, use an OutputStreamWriter and specify the Charset.

OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file, true),

"windows-1252");

writer.append(textBody);

writer.close();

Regarding your first question:

I think I know that to convert Strings to ANSI I should use:

String unicode = new String(asciiBytes, "windows-1252");

Is that correct?

Given an array of bytes representing characters in the 'windows-1252' character set, this code will construct you the appropriate Java String (internally represented as Unicode within the JVM). This isn't "converting Strings to ANSI" ... it's doing the opposite: converting 'windows-1252' to Unicode.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值