【问题标题】:Java BufferedWriter object with utf-8带有 utf-8 的 Java BufferedWriter 对象
【发布时间】:2011-10-23 08:33:43
【问题描述】:

我有以下代码,我想让输出流使用 utf-8。基本上我有像é 这样的字符,它们显示为é,所以它看起来像一个编码问题。

我见过很多使用...的例子

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

我当前的代码是...

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

是否可以将此对象定义为 UTF-8 而不必使用 OutputStreamWriter?

谢谢,

【问题讨论】:

  • 为什么不能使用 OutputStreamWriter?
  • 或者更好,为什么不能使用 XML 序列化程序?如果您使用的是 XML 序列化程序,为什么不让它处理编码?

标签: java encoding utf-8


【解决方案1】:

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

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

【讨论】:

  • 虽然这可能是解决问题的宝贵提示,但一个好的答案也可以证明解决方案。请edit 提供示例代码来说明您的意思。或者,请考虑将其写为评论。
【解决方案2】:

没有。 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,但这似乎有点极端。)

【讨论】:

  • 嗯,我做到了,但我仍然得到 é前面。这是否意味着问题在堆栈中较早?在数据库中它存储为 é 但是当我连接 jdbc:sqlserver://...... 它将找到的字段写入 é在我的档案中。无论如何都不会故意更改字符串。
  • @david99world:不清楚您的意思,但是您显示的代码中没有任何内容会为您创建 XML/HTML 实体。确实听起来问题出在其他地方。我确定在将é 写入缓冲写入器之前,您的字符串中已经包含它。
  • 问题可能是您用来读取文件的程序未配置为 utf-8。
  • 啊,非常感谢,我不知道这必须在读写时配置,谢谢:)
  • @Chris:这些天我只使用 Files.newBufferedWriter - 不需要第三方库。
【解决方案3】:

你可以使用改进的 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 ) );
  }
}

【讨论】:

    【解决方案4】:

    正如 FileWriter 的 documentation 解释的那样,

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

    尽管如此,您没有理由不能在 OutputStreamWriter 之上构建 BufferedWriter。

    【讨论】:

      猜你喜欢
      • 2016-06-09
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      • 2015-01-15
      • 2018-05-02
      • 2011-06-29
      • 2014-04-13
      相关资源
      最近更新 更多