何时使用"getResourceAsStream"方法?

Sri*_*ram 3 java properties

我使用上述方法感到困惑,因为在加载某些属性文件时,人们正在遵循不同的方法......

Properties prop 
 = new Properties(new FileInputStream(new File("<path of the properties file>"));
Run Code Online (Sandbox Code Playgroud)

很少有人在使用..

Properties prop 
 = new Properties(getClass().getResourceAsStream("<path of the properties file>"));
Run Code Online (Sandbox Code Playgroud)

哪一个使用的时候?

Nee*_*eet 8

getResourceAsStream搜索给定文件/资源​​的类路径,它还可以InputStreams从JAR内部提供资源.

因此,如果您的属性存在于物理文件系统中的某个文件夹中(例如用户文件夹,...),请使用FileInputStream并且如果该文件嵌入在您的类路径中(例如作为JAR内的资源)使用getResourceAsStream.