Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript 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: 10948
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: 21047
85
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: 21047
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!