如何用西里尔文路径阅读文件
其实我有下一个代码:
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上运行良好。 但是当它有,我得到下一个例外:
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打开流;
URL url = LauncherFrame.class.getProtectionDomain().getCodeSource().getLocation();
然后将URL传递给calculateHash
而不是文件名,并使用URL的openStream方法获取包含内容的流;
InputStream is = url.openStream(); BufferedInputStream bis = new BufferedInputStream(is); ...
- 在java中加载freemarker模板时出现FileNotFoundException
- 使用可执行Jar时找不到Spring-Boot资源
- 导出.jar时出现FileNotFoundException
- 使用File对象初始化FileInputStream时获取FileNotFoundException
- 导出为.jar文件后的FileNotFoundException
- FileNotFoundException,该文件存在Java
- 为什么我找不到我的档案?
- 引起:java.io.FileNotFoundException:无法打开ServletContext资源
- Android FileNotFoundexception – 无法从没有文件格式的图像URL获取getInputStream