How to save text files in Java with umlauts

Here at .Maui, working in Java on files written in German, we had difficulty saving files that contained umlauts. Here is the solution we found:

package javaexample;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class JavaExample {

    public static void main(String[] args) throws IOException {

        String mystring = "Baden-Württemberg";

        try (Writer dout = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream("myfile.txt"), "unicode"))) {
            dout.write(mystring);
        }
        
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *