Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, obtain an <code>InputStream</code> from which the properties are to be loaded. This can come from a number of locations, including some of the most likely:</p> <ul> <li>A <a href="http://java.sun.com/javase/6/docs/api/java/io/FileInputStream.html#FileInputStream(java.lang.String)" rel="nofollow noreferrer"><code>FileInputStream</code></a>, created with a file name that is hard-coded or specified via a <a href="http://java.sun.com/javase/6/docs/api/java/lang/System.html#getProperty(java.lang.String,%20java.lang.String)" rel="nofollow noreferrer">system property.</a> The name could be relative (to the current working directory of the Java process) or absolute.</li> <li>A resource file (a file on the classpath), obtained through a call to <code>getResourceAsStream</code> on the <a href="http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)" rel="nofollow noreferrer"><code>Class</code></a> (relative to the class file) or <a href="http://java.sun.com/javase/6/docs/api/java/lang/ClassLoader.html#getResourceAsStream(java.lang.String)" rel="nofollow noreferrer"><code>ClassLoader</code></a> (relative to the root of the class path). Note that these methods return null if the resource is missing, instead of raising an exception.</li> <li>A <a href="http://java.sun.com/javase/6/docs/api/java/net/URL.html#openStream()" rel="nofollow noreferrer"><code>URL</code></a>, which, like a file name, could be hard-coded or specified via a system property.</li> </ul> <p>Then create a new <code>Properties</code> object, and pass the <code>InputStream</code> to its <a href="http://java.sun.com/javase/6/docs/api/java/util/Properties.html#load(java.io.InputStream)" rel="nofollow noreferrer"><code>load()</code></a> method. Be sure to close the stream, regardless of any exceptions.</p> <p>In a class initializer, checked exceptions like <code>IOException</code> must be handled. An unchecked exception can be thrown, which will prevent the class from being initialized. That, in turn, will usually prevent your application from running at all. In many applications, it might be desirable to use default properties instead, or fallback to another source of configuration, such as prompting a use in an interactive context.</p> <p>Altogether, it might look something like this:</p> <pre><code>private static final String NAME = "my.properties"; private static final Properties config; static { Properties fallback = new Properties(); fallback.put("key", "default"); config = new Properties(fallback); URL res = MyClass.getResource(NAME); if (res == null) throw new UncheckedIOException(new FileNotFoundException(NAME)); URI uri; try { uri = res.toURI(); } catch (URISyntaxException ex) { throw new IllegalArgumentException(ex); } try (InputStream is = Files.newInputStream(Paths.get(uri))) { config.load(is); } catch (IOException ex) { throw new UncheckedIOException("Failed to load resource", ex); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload