java FileInputStream 在哪个文件夹中查找?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5856024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In which folder is FileInputStream looking?
提问by Alex
I use the line
我用线
FileInputStream stream = new FileInputStream("myfile.properties");
to open a properties file without specifying a path.
在不指定路径的情况下打开属性文件。
When running it on Apache Tomcat, the file can not be found. I placed the file into the root folder of the application.
在 Apache Tomcat 上运行时,找不到该文件。我将文件放入应用程序的根文件夹中。
In which folder is Java looking?
Java 在哪个文件夹中查找?
I can not change the path because the code is not by me.
我无法更改路径,因为代码不是我的。
回答by PeterMmm
It will look in the folder that is is the result of
它将在作为结果的文件夹中查找
System.getProperty("user.dir")
and this could be anywhere. That depends the current working directory when the tomcat server is started.
这可能在任何地方。这取决于启动 tomcat 服务器时的当前工作目录。
Try to load a small servlet that prints that info at loading if you cannot figure out from standard logs or the tomcat start procedure.
如果您无法从标准日志或 tomcat 启动过程中找出答案,请尝试加载一个小的 servlet,在加载时打印该信息。
回答by skaffman
It will look in the working directory of the JVM process, not the root directory of the WAR. Where that working directory is depends on how the Tomcat process was started.
它将在 JVM 进程的工作目录中查找,而不是 WAR 的根目录。该工作目录的位置取决于 Tomcat 进程的启动方式。
As such, you shouldn't do this. You should obtain references to resources from inside the WAR by asking the ServletContext
object (which has various methods to look up resource streams), e.g. from inside a Servlet:
因此,您不应该这样做。您应该通过询问ServletContext
对象(它具有各种方法来查找资源流)来从 WAR 内部获取对资源的引用,例如从 Servlet 内部:
InputStream stream = getServletContext().getResourceAsStream("myfile.properties");
Also, it's bad practice to refer to resources inside a WAR as actual files. This will only work if the WAR is exploded into a directory structure, and won't work if the servlet contain decided to run the WAR as an un-exploded .WAR
file. By sticking to the getResource...()
methods, you keep things neutral and portable.
此外,将 WAR 中的资源称为实际文件也是一种不好的做法。这仅在 WAR 分解为目录结构时才有效,如果 servlet 包含决定将 WAR 作为未分解的.WAR
文件运行,则这将不起作用。通过坚持这些getResource...()
方法,您可以保持中立和便携。
However if, as you say, you cannot change the code, then that's a problem, because the code is broken and badly written. You'll need to figure out how to launch Tomcat so that the working directory is in the "correct" place. That might entail hacking the startup scripts.
但是,如果如您所说,您无法更改代码,那么这就是一个问题,因为代码已损坏且编写得不好。您需要弄清楚如何启动 Tomcat,以便工作目录位于“正确”的位置。这可能需要破解启动脚本。
回答by corlettk
If I recall correctly:Tomcat searches the classpath for ALL resources... therefore "myfile.properties" (with no path) would be in any "top" directory in the classpath. The "default top" (if you don't specify a classpath) is WEB-INF/classes; infact I think Tomcat allways (logically) adds WEB-INF/classes to the classpath, even if you don't want it to... haven't played with Tomcat in years.
如果我没记错的话:Tomcat 在类路径中搜索所有资源......因此“myfile.properties”(没有路径)将位于类路径中的任何“顶级”目录中。“默认顶部”(如果您没有指定类路径)是 WEB-INF/classes;事实上,我认为 Tomcat 总是(逻辑上)将 WEB-INF/classes 添加到类路径中,即使您不希望它......多年没有使用 Tomcat。
回答by SJuan76
It usually works by default in the home directory.
它通常默认在主目录中工作。
In order to read properties, you should use Class.getResourceAsStream(String)
为了读取属性,您应该使用 Class.getResourceAsStream(String)
BTW, a more proper place would be META-INF dir
顺便说一句,更合适的地方是 META-INF 目录