我有一個使用一些txt文件的jar文件。爲了得到它們,它使用Class.getResourceAsStream
函數。getResourceAsStream不適用於每個類?
Class A
{
public InputStream getInputStream(String path) throws Exception {
try {
return new FileInputStream(path);
} catch (FileNotFoundException ex) {
InputStream inputStream = getClass().getResourceAsStream(path);
if (inputStream == null)
throw new Exception("Failed to get input stream for file " + path);
return inputStream;
}
}
}
此代碼工作正常。
問題是,如果我將類A定義爲extends java.io.File
,則我從getResourceAsStream
獲得的InputStream爲null。
而且,如果我離開A級爲常規類(非繼承),並定義B級爲:
Class B extends java.io.File
{
public InputStream getInputStream(String path) throws Exception
{
return new A().getInputStream(path);
}
}
返回的InputStream仍然是空。
什麼問題?有沒有辦法從繼承File
的類訪問該文件?
感謝,
你說得對。問題是不同的。看到我的答案。謝謝! – Dikla 2009-05-17 08:23:50