わかりましたので、ユーザーがjarファイルに含まれるファイル名を入力するプログラムを作成しているため、プログラムはそのファイルをjarファイルから抽出して開きます。これを行うのに問題はありません。奇妙なことがありますここで使用しているコードがわかりません:
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;
}
私の質問は getpath メソッドに関するものです。必要ではないと思います。Java には getClass().getProtectionDomain().getCodeSource().getLocation().getPath())= を行うメソッドが組み込まれていると思います。 "/C:/test%20test/" は "\C:\test test\" に変換されるので、基本的には getpath メソッドが '\' を '/' に、%20 を' '