[英]Access File in bin folder of Java web application
我想從class activitydata的src文件夾中讀取一個文本文件。 為了做到這一點,我編寫了以下代碼
ActivityData c = new ActivityData();
Class<? extends ActivityData> cls = c.getClass();
URL url = cls.getResource("file.txt");
System.out.println("Value = " + url);
盡管我在控制台中獲得了url,但是當我編寫以下代碼時卻出現了異常:
Gson gson = new Gson();
JsonReader reader = new JsonReader(new FileReader(url.toString()));
例外:
java.io.FileNotFoundException: file:\G:\Study%20Folder.......\file.txt (The filename, directory name, or volume label syntax is incorrect)
在Java Web應用程序中,不能假設您將以File
的形式訪問資源。 請改用getResourceAsStream
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.