简体   繁体   English

使用utf-8的Java BufferedWriter对象

[英]Java BufferedWriter object with utf-8

I have the following code and I want to make the outputstream use utf-8. 我有以下代码,我想使输出流使用utf-8。 Basically I have characters like é that appear as é 基本上我有像é这样的字符,显示为é so it looks like an encoding issue. 所以它看起来像一个编码问题。

I've seen lots of examples that use... 我见过很多使用的例子......

OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(path),"UTF-8");

My current code though is... 我目前的代码是......

BufferedWriter out = new 
BufferedWriter(new FileWriter(DatabaseProps.fileLocation + "Output.xml"));

Is it possible to define this object as UTF-8 without having to use the OutputStreamWriter? 是否可以将此对象定义为UTF-8而无需使用OutputStreamWriter?

Thanks, 谢谢,

No. FileWriter doesn't let you specify the encoding, which is extremely annoying. FileWriter不允许您指定编码,这非常烦人。 It always uses the system default encoding. 它始终使用系统默认编码。 Just suck it up and use OutputStreamWriter wrapping a FileOutputStream . 只需将其吸收并使用OutputStreamWriter包装FileOutputStream You can still wrap the OutputStreamWriter in a BufferedWriter of course: 您当然可以将OutputStreamWriter包装在BufferedWriter中:

BufferedWriter out = new BufferedWriter
    (new OutputStreamWriter(new FileOutputStream(path), StandardCharsets.UTF_8));

Or as of Java 8: 或者从Java 8开始:

BufferedWriter out = Files.newBufferedWriter(Paths.of(path));

(Of course you could change your system default encoding to UTF-8, but that seems a bit of an extreme measure.) (当然,您可以将系统默认编码更改为UTF-8,但这似乎有点极端。)

You can use improved FileWriter, improved by me. 您可以使用改进的FileWriter,由我改进。

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

/**
 * Created with IntelliJ IDEA.
 * User: Eugene Chipachenko
 * Date: 20.09.13
 * Time: 10:21
 */
public class BufferedFileWriter extends OutputStreamWriter
{
  public BufferedFileWriter( String fileName ) throws IOException
  {
    super( new FileOutputStream( fileName ), Charset.forName( "UTF-8" ) );
  }

  public BufferedFileWriter( String fileName, boolean append ) throws IOException
  {
    super( new FileOutputStream( fileName, append ), Charset.forName( "UTF-8" ) );
  }

  public BufferedFileWriter( String fileName, String charsetName, boolean append ) throws IOException
  {
    super( new FileOutputStream( fileName, append ), Charset.forName( charsetName ) );
  }

  public BufferedFileWriter( File file ) throws IOException
  {
    super( new FileOutputStream( file ), Charset.forName( "UTF-8" ) );
  }

  public BufferedFileWriter( File file, boolean append ) throws IOException
  {
    super( new FileOutputStream( file, append ), Charset.forName( "UTF-8" ) );
  }

  public BufferedFileWriter( File file, String charsetName, boolean append ) throws IOException
  {
    super( new FileOutputStream( file, append ), Charset.forName( charsetName ) );
  }
}

As the documentation for FileWriter explains, 正如FileWriter的文档所解释的那样,

The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. 此类的构造函数假定默认字符编码和默认字节缓冲区大小是可接受的。 To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream. 要自己指定这些值,请在FileOutputStream上构造OutputStreamWriter。

There's no reason you can't construct your BufferedWriter on top of the OutputStreamWriter though. 没有理由你不能在OutputStreamWriter之上构造你的BufferedWriter。

Use the method, Files.newBufferedWriter(Path path, Charset cs, OpenOption... options) 使用方法Files.newBufferedWriter(Path path,Charset cs,OpenOption ... options)

As requested by Toby, here is the sample code. 根据Toby的要求,这是示例代码。

String errorFileName = (String) exchange.getIn().getHeader(HeaderKey.ERROR_FILE_NAME.getKey());
        String errorPathAndFile = dir + "/" + errorFileName;
        writer = Files.newBufferedWriter(Paths.get(errorPathAndFile),  StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW);
        try {
            writer.write(MESSAGE_HEADER + "\n");
        } finally {
            writer.close();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM