Encoding test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.nio.charset.Charset; | |
public class Main { | |
public static void main(String[] args) throws IOException { | |
System.out.println("file.encoding=" + System.getProperty("file.encoding")); | |
System.out.println("sun.jnu.encoding=" + System.getProperty("sun.jnu.encoding")); | |
// U+0100 == LATIN CAPITAL LETTER A WITH MACRON | |
String filename = "test - \u0100dam"; | |
File outputFile = new File(filename); | |
// Create a file with a UTF-8 name. | |
FileOutputStream fos = new FileOutputStream(outputFile); | |
try { | |
byte[] contents = filename.getBytes(Charset.forName("UTF-8")); | |
fos.write(contents); | |
} finally { | |
if (fos != null) { | |
fos.close(); | |
} | |
} | |
System.out.println("Created \"" + filename + "\""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a sample run on my mac (OS 10.6).
So, the output says "?" as the character can't be represented in MacRoman, but the filename is correctly interpreted as UTF-8.
Now, trying again with
file.encoding
:And once more with
sun.jnu.encoding
.