這裏第一次發帖,會盡量簡潔。這是一個經典的「無法訪問Applet文件」的問題,但我遇到了一些特殊的困難。如何使用getCodeBase()在Java Applet中查找並加載文件?
我想這個文件改寫:
到模板的小程序加載libpd在PureData的(puredata.info)製造(https://github.com/libpd/libpd)補丁程序......這個已經工作在正常在非小應用程序的Java程序(見上文),其中主功能發現使用所述貼片主要功能:
PdBase.openAudio(0, outChans, (int) sampleRate);
int patch = PdBase.openPatch("samples/com/noisepages/nettoyeur/libpd/sample/test.pd");
PdBase.computeAudio(true);
它嘗試文件加載路徑並進入int變量的原因是THA T中的核心函數本身做這個有:
public synchronized static int openPatch(File file) throws IOException {
if (!file.exists()) {
throw new FileNotFoundException(file.getPath());
}
String name = file.getName();
File dir = file.getParentFile();
long ptr = openFile(name, (dir != null) ? dir.getAbsolutePath() : ".");
if (ptr == 0) {
throw new IOException("unable to open patch " + file.getPath());
}
int handle = getDollarZero(ptr);
patches.put(handle, ptr);
return handle;
}
public synchronized static int openPatch(String path) throws IOException {
return openPatch(new File(path));
}
這是因爲PD試圖通過給一個int「把手」(dollarZero,遺留原因),以確定每個補丁,使INT手柄被傳來傳去打開並關閉修補程序文件。
所以現在。我試圖在Applet中加載相同的文件,因爲我相信它在客戶端運行並且不知道我在說什麼路徑,所以我在java.net.URL上閱讀並嘗試構建的變化:
patchURL = new URL("test.pd");
PdBase.openPatch(patchURL.getPath().toString());
和
URL url = this.getClass().getResource("test.pd");
通過previous questions在init啓發()和start()小應用程序的功能,把原始主成局部靜態方法的聲音()。
我得到的只是空指針。我會想我需要的只是一個簡單的getDocumentBase(),但似乎無法使它工作。任何人?
爲了跟進,做'String pd = this.getCodeBase()。toString();'然後'pdPath = pd +「../ samples/com/noisepages/nettoyeur/libpd/sample/test.pd」 ;'把我帶到了正確的路徑(當然這不是完全的代碼路徑)......但是在這一點上我得到'文件未找到',這與'System.out.println(pdPath) ;'給我的文件的確切路徑,我可以看到它! – pland 2012-02-04 15:16:10
是否有'openPatch(URL)'或'openPatch(InputStream)'方法?如果不是,該API將無法使用小程序。 – 2012-02-04 15:17:03
不,並且不夠公平。如果我溝通小程序(我試圖給一羣音頻學生提供使用libpd和Java的最簡單可能的途徑),可以通過面板來做到這一點嗎? – pland 2012-02-04 15:20:04