filewriter 设置编码_使用FileWriter(Java)以UTF-8编写文件?

I have the following code however, I want it to write as a UTF-8 file to handle foreign characters. Is there a way of doing this, is there some need to have a parameter?

I would really appreciate your help with this. Thanks.

try {

BufferedReader reader = new BufferedReader(new FileReader("C:/Users/Jess/My Documents/actresses.list"));

writer = new BufferedWriter(new FileWriter("C:/Users/Jess/My Documents/actressesFormatted.csv"));

while( (line = reader.readLine()) != null) {

//If the line starts with a tab then we just want to add a movie

//using the current actor's name.

if(line.length() == 0)

continue;

else if(line.charAt(0) == '\t') {

readMovieLine2(0, line, surname.toString(), forename.toString());

} //Else we've reached a new actor

else {

readActorName(line);

}

}

} catch (IOException e) {

e.printStackTrace();

}

解决方案

Safe Encoding Constructors

Getting Java to properly notify you of encoding errors is tricky. You must use the most verbose and, alas, the least used of the four alternate contructors for each of InputStreamReader and OutputStreamWriter to receive a proper exception on an encoding glitch.

For file I/O, always make sure to always use as the second argument to both OutputStreamWriter and InputStreamReader the fancy encoder argument:

Charset.forName("UTF-8").newEncoder()

There are other even fancier possibilities, but none of the three simpler possibilities work for exception handing. These do:

OutputStreamWriter char_output = new OutputStreamWriter(

new FileOutputStream("some_output.utf8"),

Charset.forName("UTF-8").newEncoder()

);

InputStreamReader char_input = new InputStreamReader(

new FileInputStream("some_input.utf8"),

Charset.forName("UTF-8").newDecoder()

);

As for running with

$ java -Dfile.encoding=utf8 SomeTrulyRemarkablyLongcLassNameGoeShere

The problem is that that will not use the full encoder argument form for the character streams, and so you will again miss encoding problems.

Longer Example

Here’s a longer example, this one managing a process instead of a file, where we promote two different input bytes streams and one output byte stream all to UTF-8 character streams with full exception handling:

// this runs a perl script with UTF-8 STD{IN,OUT,ERR} streams

Process

slave_process = Runtime.getRuntime().exec("perl -CS script args");

// fetch his stdin byte stream...

OutputStream

__bytes_into_his_stdin = slave_process.getOutputStream();

// and make a character stream with exceptions on encoding errors

OutputStreamWriter

chars_into_his_stdin = new OutputStreamWriter(

__bytes_into_his_stdin,

/* DO NOT OMIT! */ Charset.forName("UTF-8").newEncoder()

);

// fetch his stdout byte stream...

InputStream

__bytes_from_his_stdout = slave_process.getInputStream();

// and make a character stream with exceptions on encoding errors

InputStreamReader

chars_from_his_stdout = new InputStreamReader(

__bytes_from_his_stdout,

/* DO NOT OMIT! */ Charset.forName("UTF-8").newDecoder()

);

// fetch his stderr byte stream...

InputStream

__bytes_from_his_stderr = slave_process.getErrorStream();

// and make a character stream with exceptions on encoding errors

InputStreamReader

chars_from_his_stderr = new InputStreamReader(

__bytes_from_his_stderr,

/* DO NOT OMIT! */ Charset.forName("UTF-8").newDecoder()

);

Now you have three character streams that all raise exception on encoding errors, respectively called chars_into_his_stdin, chars_from_his_stdout, and chars_from_his_stderr.

This is only slightly more complicated that what you need for your problem, whose solution I gave in the first half of this answer. The key point is this is the only way to detect encoding errors.

Just don’t get me started about PrintStreams eating exceptions.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值