具有utf-8的Java BufferedWriter对象

具有utf-8的Java BufferedWriter对象,java,encoding,utf-8,Java,Encoding,Utf 8,我有以下代码,我想让outputstream使用utf-8。基本上我有像é这样的字符,显示为和#233所以看起来像是编码问题 我见过很多使用 OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(path),"UTF-8"); 我现在的代码是 BufferedWriter out = new BufferedWriter(new FileWriter(DatabaseProps.fileLocation +

我有以下代码,我想让outputstream使用utf-8。基本上我有像
é
这样的字符,显示为
和#233
所以看起来像是编码问题

我见过很多使用

OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(path),"UTF-8");
我现在的代码是

BufferedWriter out = new 
BufferedWriter(new FileWriter(DatabaseProps.fileLocation + "Output.xml"));
是否可以将此对象定义为UTF-8而不必使用OutputStreamWriter


谢谢,

否。
FileWriter
不允许您指定编码,这非常烦人。它始终使用系统默认编码。只需将其吸收并使用
OutputStreamWriter
包装
FileOutputStream
。当然,您仍然可以将OutputStreamWriter包装在BufferedWriter中:

BufferedWriter out = new BufferedWriter
    (new OutputStreamWriter(new FileOutputStream(path), StandardCharsets.UTF_8));
或者从Java 8开始:

BufferedWriter out = Files.newBufferedWriter(Paths.of(path));
(当然,您可以将系统默认编码更改为UTF-8,但这似乎有点极端。)

正如for FileWriter所解释的

此类的构造函数假定默认字符编码和默认字节缓冲区大小是可接受的。要自己指定这些值,请在FileOutputStream上构造OutputStreamWriter


但是,您没有理由不能在OutputStreamWriter之上构建BufferedWriter。

您可以使用改进的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 ) );
  }
}
使用Files.newBufferedWriter方法(路径路径、字符集cs、OpenOption…选项)

根据托比的要求,以下是示例代码

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();
        }

为什么不能使用OutputStreamWriter?或者更好,为什么不能使用XML序列化程序?如果您使用的是XML序列化程序,为什么不让它处理编码呢?嗯,我这样做了,但我仍然得到é;弗雷。这是否意味着问题出现在堆栈的早期?在db中,它存储为é,但当我连接jdbc时:sqlserver://...... 它将找到的字段写入为é;在我的档案里。无论如何,字符串不是有意更改的。@david99world:不清楚您的意思,但您所展示的代码中没有任何内容将为您创建XML/HTML实体。听起来问题确实出在别的地方。我相信你已经有了
é。问题可能是您用于读取文件的程序未配置为utf-8。啊,非常感谢,我不知道这必须在读取和写入时配置,谢谢:)@Chris:这些天我只使用Files.newBufferedWriter-不需要第三方库。虽然这可能是解决问题的一个有价值的提示,但一个好的答案也说明了解决方法。请提供示例代码来说明您的意思。或者,考虑将此写入评论。