Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do i load a property file from URL?  RSS feed

 
Thennam Pandian
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IS it possible to load a property file from URL?
 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at java.net.URLConnection getInputStream(), java.util.Properties load(), java.io.Reader, java.io.InputStreamReader
 
Thennam Pandian
Ranch Hand
Posts: 163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Lacar.

The working code snippet is below.

URL url = new URL("url");
InputStream in = url.openStream();

Properties prop = new Properties();
prop.load(in);

System.out.println( prop.getProperty("key"));
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That will work if the properties file uses an encoding compatible with the system encoding. If it doesn't, you need to specify it explicitly:
Note that I also use a try-finally block to make sure the Reader (and thereby the InputStream) is properly closed when we're done with it.
 
Thennam Pandian
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect code Rob Spoor.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!