2016-11-19 20 views
2

私の等級javaプロジェクトの構造は次のとおりです。プロパティファイルから読み込みおよび読み込めません。

ご覧のとおり、リソースフォルダは、クラスパスにあります。

しかし、私はJavaのフォルダ

new File("somefile.txt").exists() 

下のクラスに次のように実行したとき、私はFileNotFoundExceptionを取得します。

なぜこのファイルにアクセスできないのかを知る助けになるでしょうか。 これはクラスパスにあります。あなたが使用することができます

enter image description here

+0

からそれを見つけた

Properties prop = new Properties(); InputStream inputStream = getClass().getClassLoader().getResourceAsStream("somefile.txt"); if (inputStream != null) { prop.load(inputStream); } else { throw new FileNotFoundException("Property file '" + fileName + "' not found in the classpath"); } 

の下のようなあなたの問題を解決することができます。ビルドフォルダを削除する+ intellijキャッシュ+グラデーションリフレッシュを無効にする(古いアーティファクトを削除した後) –

答えて

0

ClassLoader classLoader = getClass().getClassLoader(); 
String filePath= classLoader.getResource("filename").getFile(); 
new File(filePath).exists(); 

詳細については、thisチュートリアルをご覧ください。

+0

classLoader.getResource( "filename")。getFile(); - 私は昨日、javaのドキュメントを読んでいますが、あなたの答えを受け入れることで得ました。 – sreeprasad

+0

乾杯、ありがとう:) – mallaudin