本文介绍了使用FileWriter(Java)以UTF-8编写文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

然而我有下面的代码,我想把它写成一个UTF-8文件来处理外来字符。有没有办法做到这一点,有一些需要有一个参数?



我真的很感激你的帮助。

  try {
BufferedReader reader = new BufferedReader(new FileReader(C:/ Users / Jess / My文档/ actresses.list));
writer = new BufferedWriter(new FileWriter(C:/ Users / Jess / My Documents / actressesFormatted.csv));
while((line = reader.readLine())!= null){
//如果行以tab开始,那么我们只想添加一个电影
//使用当前演员的名字。
if(line.length()== 0)
continue;
else if(line.charAt(0)=='\t'){
readMovieLine2(0,line,surname.toString(),forename.toString());
} //否则我们已经到达一个新的演员
else {
readActorName(line);

$ b} catch(IOException e){
e.printStackTrace();

$ / code $ / $ p

解决方案

安全编码构造函数



让Java正确地通知编码错误是棘手的。对于每个 InputStreamReader ,您必须使用四个备用构造器的最详细的 / code>和 OutputStreamWriter 在编码故障中接收适当的异常。

对于文件I / O,总是确保始终用作第二个参数给 OutputStreamWriter InputStreamReader 花式编码器参数:

  p $ p> 

还有其他的更多的可能性,但是三种更简单的可能性都不适用于异常处理。它们是:
$ b $ pre $ Output $ Output $ b charset.forName(UTF-8)。newEncoder()
);

InputStreamReader char_input = new InputStreamReader(
FileInputStream(some_input.utf8),
Charset.forName(UTF-8)。newDecoder()
);

至于

  $ java -Dfile.encoding = utf8 SomeTrulyRemarkablyLongcLassNameGoeShere 





更长的例子



这是一个更长的例子,它管理一个进程而不是一个文件,我们将两个不同的输入字节流和一个输出字节流全部转换为UTF-8字符流完整的异常处理

  //这个运行一个带有UTF-8 STD的perl脚本{IN,OUT,ERR } streams 
Process
slave_process = Runtime.getRuntime()。exec(perl -CS script args);

//获取stdin字节流...
OutputStream
__bytes_into_his_stdin = slave_process.getOutputStream();

//创建一个带有编码错误异常的字符流
OutputStreamWriter
chars_into_his_stdin = new OutputStreamWriter(
__bytes_into_his_stdin,
/ *不要OMIT! * / Charset.forName(UTF-8)。newEncoder()
);

//获取他的stdout字节流...
InputStream
__bytes_from_his_stdout = slave_process.getInputStream();

//创建一个字符流,编码错误异常
InputStreamReader
chars_from_his_stdout = new InputStreamReader(
__bytes_from_his_stdout,
/ *不要OMIT! * / Charset.forName(UTF-8)。newDecoder()
);

//获取他的stderr字节流...
InputStream
__bytes_from_his_stderr = slave_process.getErrorStream();

//创建一个带有编码错误异常的字符流
InputStreamReader
chars_from_his_stderr = new InputStreamReader(
__bytes_from_his_stderr,
/ *不要OMIT! * / Charset.forName(UTF-8)。newDecoder()
);

现在您有三个字符流,都会引发编码错误,分别称为 chars_into_his_stdin chars_from_his_stdout chars_from_his_stderr

这个问题稍微复杂一点,你的问题需要解决,我在这个答案的前半部分给出了解决方案。关键是这是检测编码错误的唯一方法。



不要让我开始关于 PrintStream s饮食例外。


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.

这篇关于使用FileWriter(Java)以UTF-8编写文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:39