The best international Java alternative to getClass (). GetResource ()

I have many resource files bundled with my Java application. These files have file names containing international characters, such as ü or æ. I would like to load these files using getClass (). GetResource (), but apparently this is not supported, because for these specific file names, the getResource method always returns null.

This made me experiment using the URL character encoding of international characters, but this is not supported as indicated by http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4968789 .

So my question is: what is the recommended way to load a resource with a name containing international characters? For example, I need to upload the contents of a UTF-8 file named Sjælland.txt

+7
source share
2 answers

Not sure if there is a best (probably this is a candidate for worst , because it's pretty hack), but it seems like a capable mechanism. This results in the need to use getResource , reading the jar directly.

 public class NavelGazing { public static void main(String[] args) throws Throwable { // Do a little navel gazing. java.net.URL codeBase = NavelGazing.class.getProtectionDomain().getCodeSource().getLocation(); // Must be a jar. if (codeBase.getPath().endsWith(".jar")) { // Open it. java.util.jar.JarInputStream jin = new java.util.jar.JarInputStream(codeBase.openStream()); // Walk the entries. ZipEntry entry; while ((entry = jin.getNextEntry()) != null ) { System.out.println("Entry: "+entry.getName()); } } } } 

I added a file called Sjælland.txt , and it successfully received the entry.

+3
source

I'm not sure I understand you correctly, but if I try

 URL url = Test.class.getResource("/Sjælland.txt"); Object o = url.getContent(); 

then o is sun.net.www.content.text.PlainTextInputStream .

I am using JDK 1.6 on a windows machine. I have (by default?) System.property sun.jnu.encoding installed on Cp1252. So everything is working fine. JDK 1.4 bug fixed. Perhaps this is what you are using.

0
source

All Articles