2012-09-06 50 views
3

其實我有下面的代碼:如何閱讀使用西里爾路徑文件

String f = LauncherFrame.class.getProtectionDomain().getCodeSource().getLocation().getPath(); // path to launcher 
java.lang.System.out.println(f); 

String launcherHash = ""; 

try{ 
    MessageDigest md5 = MessageDigest.getInstance("MD5"); 
    launcherHash = calculateHash(md5, f); 
}catch (Exception e) { 
    java.lang.System.out.println(e){ 
    return; 
} 

calculateHash功能:

public static String calculateHash(MessageDigest algorithm,String fileName) throws Exception{ 
     FileInputStream fis = new FileInputStream(fileName); 
     BufferedInputStream bis = new BufferedInputStream(fis); 
     DigestInputStream dis = new DigestInputStream(bis, algorithm); 

     while (dis.read() != -1); 
      byte[] hash = algorithm.digest(); 

     return byteArray2Hex(hash); 
} 

它的工作好於UNIX/Windows的時候,我.jar文件都沒有西里爾字符在路徑中。但是當它有,我得到下一個例外:

java.io.FileNotFoundException: 
C:\Users\%d0%90%d1%80%d1%82%d1%83%d1%80\Documents\NetBeansProjects\artcraft-client\build\classes (Can't find file) 

我該如何解決它?

回答

2

這是來自內存,所以語法可能有點偏離,但最好的可能是使用內置的URL支持直接從URL打開流;

URL url = LauncherFrame.class.getProtectionDomain().getCodeSource().getLocation(); 

然後將URL傳遞給calculateHash而不是文件名,並使用URL's openStream method獲得與內容流;

InputStream is = url.openStream(); 
BufferedInputStream bis = new BufferedInputStream(is); 
...