简体   繁体   English

使用getResourceAsStream()方法从远程位置读取属性文件

[英]Reading a property file from a remote location using getResourceAsStream() method

Can I use the below code to read a propery file from a remote location in internet? 我可以使用以下代码从Internet的远程位置读取属性文件吗?

InputStream is = getClass().getResourceAsStream("/filename.properties");
Properties pro = new Properties();
pro.load(is);
// ...

Can I replace the /filename.properties in to a remote path like 192.168.173.238/D:/war/filename.properties ? 我可以将/filename.properties替换为192.168.173.238/D:/war/filename.properties之类的远程路径吗? If yes, how should I specify the path? 如果是,我应该如何指定路径? This code is running in Apache Tomcat. 此代码在Apache Tomcat中运行。

If it's available by an URL (like as you would enter in your webbrowser's address bar), use URL#openStream() or URLConnection#getInputStream() . 如果URL可用(如您在URL#openStream()浏览器的地址栏中输入的那样),请使用URL#openStream()URLConnection#getInputStream()

InputStream is = new URL(url).openStream();

If it's available by a local disk file system path (like as you would enter in your disk explorer's address bar), use FileInputStream . 如果本地磁盘文件系统路径可用(如您在磁盘资源管理器的地址栏中输入的那样),请使用FileInputStream

InputStream is = new FileInputStream(path);

Simple as that. 就那么简单。

Your next question shall probably be, "What's the right URL or disk file system path?" 您的下一个问题可能是“正确的URL或磁盘文件系统路径是什么?” . This can't be answered based on the information provided so far. 根据目前提供的信息无法回答。

Hi Can I use the below code to read a propery file from a remote location in internet. 您好:我可以使用以下代码从Internet的远程位置读取属性文件。

Probably not. 可能不是。 You can only use it to read a property file that is available on the CLASSPATH. 您只能使用它来读取CLASSPATH上可用的属性文件。 Unless you are using something like the RMI codebase feature, or URLClassLoaders, your CLASSPATH does not include network locations. 除非您使用RMI代码库功能或URLClassLoaders之类的东西,否则CLASSPATH不包含网络位置。

You don't seem to understand what getResourceAsStream() is actually for. 您似乎并不了解getResourceAsStream()的实际用途。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM