如何用西里尔文路径阅读文件 [英] How to read file with cyrillic path

查看:153
本文介绍了如何用西里尔文路径阅读文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其实我有下一个代码:

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

当我的.jar文件没有西里尔语时,它在unix / windows上运行良好路径中的字符。但是当它有,我得到下一个例外:

It's work good on unix/windows when my .jar file haven't cyrillic characters in path. But when it have, I getting next exception:

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)

我如何解决?

推荐答案

这是来自内存所以语法可能有点偏,但最好的可能是使用内置的URL支持直接从URL打开流;

This is from memory so the syntax may be a bit off, but the best is probably to use the built in URL support for opening streams directly from an URL;

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

然后将URL传递给 calculateHash 而不是文件名并使用 URL的openStream方法获取包含内容的流;

then pass the URL to calculateHash instead of the filename and use URL's openStream method to get a stream with the content;

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

这篇关于如何用西里尔文路径阅读文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆