1. 程式人生 > >Java 讀取jar內的檔案

Java 讀取jar內的檔案


 
private static String jarPath = Encrypt.class.getProtectionDomain().getCodeSource().getLocation().getFile();
URL url=new URL("jar:file:" + jarPath + "!/a.txt");
InputStream fis = url.openStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int read = -1;
while 
((read = fis.read(buf)) > 0) { bos.write(buf,0,read); } String base64Str = new String(bos.toByteArray());