44

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,

2
  • Why can't you use an OutputStreamWriter? Commented Aug 9, 2011 at 15:44
  • Or better, why can't you use an XML serializer? And if you are using an XML serializer, why aren't you letting it handle the encoding? Commented Aug 9, 2011 at 16:36

4 Answers 4

153

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.get(path));

(Of course, you could change your system default encoding to UTF-8, but that seems a bit of an extreme measure.)

Sign up to request clarification or add additional context in comments.

6 Comments

Hmm, I did that but I still get é for é. Does that mean the issue is earlier in the stack? In the db it's stored as é but when I connect with jdbc:sqlserver://...... it writes the found field as é in my file. The string is not intentionally being changed in anyway.
@david99world: It's not clear what you mean, but nothing in the code you've shown is going to be creating XML/HTML entities for you. It does indeed sound like the problem is somewhere else. I'm sure you'll already have the é in your string before you write it to the buffered writer.
The problem might that the program you are using to read your file is not configured for utf-8.
Ah, thank you very much, I wasn't aware this had to be configured at read as well as write, thank you :)
@Chris: Well these days I'd just use Files.newBufferedWriter - no need for a third party lib.
|
5

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

Comments

3

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.

Comments

1

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

1 Comment

While this might be a valuable hint to solve the problem, a good answer also demonstrates the solution. Please edit to provide example code to show what you mean. Alternatively, consider writing this as a comment instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.