ok so I am making a program where the user types a file name that is included in the jar file, so the program extracts that file from the jar then opens it.I have no problem doing any of this, there is just something weird that I don't understand here is the code I'm using:
String path=getpath(getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
public void open(String filename) throws FileNotFoundException, IOException{
InputStream is = getClass().getResourceAsStream("pdfs/"+filename);
OutputStream os = new FileOutputStream(path+"SultanKadab.pdf");
byte[] buffer = new byte[4096];
int length;
while ((length = is.read(buffer)) > 0)
os.write(buffer, 0, length);
os.close();
is.close();
if (pdfFile.exists()&&Desktop.isDesktopSupported())
Desktop.getDesktop().open(pdfFile); //opening file
}
public String getpath(String f){
String s = "";
int lastInd=0;
for(int i=0;i<f.length();i++){
if(f.charAt(i)=='/'){s+="\\";lastInd=s.length()-1;}
else if(f.charAt(i)=='%'&&f.length()-i>=2){
if(f.charAt(i+1)=='2'&&f.charAt(i+2)=='0')i+=2;s+=" ";
}
else s+=f.charAt(i);
}
f=s.substring(0,lastInd+1);
return f;
}
my question is about the getpath method, I don't think it is necessary, I think that java has a built in method to do that getClass().getProtectionDomain().getCodeSource().getLocation().getPath())="/C:/test%20test/" it is converted to "\C:\test test\" so I can pass it to the fileOutputStream constructor basically the getpath method changes the '\' to '/' and the %20 to ' '