何时使用"getResourceAsStream"方法? [英] When to use "getResourceAsStream" method?
问题描述
我对使用上述方法感到困惑,因为在加载某些属性文件时,人们遵循的是不同的方法...
I was confused using the said method because while loading some properties file people are following a different approaches...
Properties prop
= new Properties(new FileInputStream(new File("<path of the properties file>"));
很少有人在使用..
Properties prop
= new Properties(getClass().getResourceAsStream("<path of the properties file>"));
何时使用哪个?
推荐答案
getResourceAsStream
在类路径中搜索给定的文件/资源,它还可以提供 InputStreams
的资源在一个JAR中.
getResourceAsStream
searchs you classpath for the given file/resource and it can also provide InputStreams
of resources from inside a JAR.
因此,如果您的属性存在于物理文件系统中的某个文件夹中(例如,用户文件夹等),请使用 FileInputStream
,并且如果该文件已嵌入到您的类路径中(例如,作为文件内部的资源)JAR)使用 getResourceAsStream
.
So, if your properties exist in some folder in the physical filesystem (e.g. user folder, ...) use FileInputStream
and if the file is embedded in your classpath (e.g. as a resource inside the JAR) use getResourceAsStream
.
这篇关于何时使用"getResourceAsStream"方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!