Java BufferedWriter object with utf-8

2020-01-26 06:11发布

I have the following code and I want to make the outputstream use 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?

Thanks,

4条回答
疯言疯语
2楼-- · 2020-01-26 06:30

You can use improved FileWriter, improved by me.

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 ) );
  }
}
查看更多
聊天终结者
3楼-- · 2020-01-26 06:32

No. FileWriter doesn't let you specify the encoding, which is extremely annoying. It always uses the system default encoding. Just suck it up and use OutputStreamWriter wrapping a FileOutputStream. You can still wrap the OutputStreamWriter in a BufferedWriter of course:

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

Or as of 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.)

查看更多
一纸荒年 Trace。
4楼-- · 2020-01-26 06:32

As the documentation for FileWriter explains,

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.

There's no reason you can't construct your BufferedWriter on top of the OutputStreamWriter though.

查看更多
祖国的老花朵
5楼-- · 2020-01-26 06:37

Use the method, Files.newBufferedWriter(Path path, Charset cs, OpenOption... options)

As requested by Toby, here is the sample code.

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();
        }
查看更多
登录 后发表回答