[英]The filename, directory name, or volume label syntax is incorrect - how to specify file path in properties file
我正在讀取屬性文件以獲取如下文件路徑。
String result = "";
InputStream inputStream = null;
try {
Properties prop = new Properties();
String propFileName = "config.properties";
inputStream = GetPropertyValues.class.getClassLoader().getResourceAsStream(propFileName);
if (inputStream != null) {
prop.load(inputStream);
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
屬性文件具有如下指定的路徑。
configSettingsFilePath = C:\\\\ConfigSetting.xml
現在,當我運行代碼說找不到文件時,出現以下異常。
Creating instance of bean 'configSettingHelper'
configSettingsFilePath = C:\\ ConfigSetting.xml 2017-09-18 14:47:00調試ConfigSettingHelper:42-ConfigSettingHelper :: ConfigSetting File:configSettingsFilePath = C:\\ ConfigSetting.xml javax.xml.bind.UnmarshalException-具有鏈接的異常:[ java.io.FileNotFoundException:C:\\ Java \\ eclipse \\ eclipse \\ configSettingsFilePath = C:\\ ConfigSetting.xml(文件名,目錄名或卷標簽語法不正確)]在com.sun.xml.internal.bind.v2 java.xml.bind.help上的.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:246)位於com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214) .java.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157),javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.unmarshal)(AbstractUnmarshallerImpl.java:162)。 javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerI mpl.java:189)
如果我直接在代碼中使用“ C:\\ ConfigSetting.xml”,則無需讀取屬性文件的路徑,而是讀取文件。
您能否建議在屬性文件中使用什么來指定路徑?
僅在.jar運行時,讀取文件失敗。 從Netbeans中運行該應用程序很好。 (不同的路徑)
而且,路徑來自
URL resourceURL = MyClass.class.getResource("mydir/myfile.txt");
打印出路徑是完美的。
另外,在同一目錄中的mypicture.gif可以很好地加載:
ImageIcon mypicture = new ImageIcon(imageURL, description)).getImage();
即使運行.jar。 IE:實際路徑必須正確。
這只是我嘗試通過閱讀的文本文件
InputStream input = new FileInputStream(fileName);
是失敗的時間-僅當它在罐子里時。
這可能是因為C:\\
不在類路徑中。 您正在使用getClassLoader()
,它大概會返回ClassLoader
。
根據 ClassLoader#getResource
的文檔 :
此方法將首先在父類加載器中搜索資源; 如果父級為null,則搜索虛擬機內置的類加載器的路徑。 失敗的話,此方法將調用findResource(String)來查找資源。
該文件位於驅動器的根目錄中,該根目錄將不在類路徑或VM內置的類加載器的路徑中。 如果失敗,則findResource
是后備。 尚不清楚findResource
在findResource
看不到實現,但似乎並不注意C:
findResource
解決方案是將屬性文件移到您的類路徑中。 通常,您會將這樣的屬性文件放在src/main/resources
文件夾中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.