try { PropertiesConfiguration pcf = new PropertiesConfiguration(); FileHandler fh = new FileHandler(pcf); fh.setFileName(fname); fh.load(); Configuration cf = pcf.interpolatedConfiguration(); LOG.info("Loaded properties from {}", fname);
/** * Saves the associated file to the specified URL. This does not change the * location of this object (use {@link #setURL(URL)} if you need it). * * @param url the URL * @throws ConfigurationException if an error occurs during the save * operation */ public void save(final URL url) throws ConfigurationException { save(url, checkContentAndGetLocator()); }
/** * Updates the {@code FileHandler}. This method is called by the builder * when a new configuration instance was created which is associated with a * new file handler. It updates the internal file handler reference and * performs necessary listener registrations. * * @param fh the new {@code FileHandler} (can be <b>null</b>) */ public synchronized void updateFileHandler(final FileHandler fh) { if (handler != null) { handler.removeFileHandlerListener(this); } if (fh != null) { fh.addFileHandlerListener(this); } handler = fh; }
/** * Loads the associated file from the specified stream, using the encoding * returned by {@link #getEncoding()}. * * @param in the input stream * @throws ConfigurationException if an error occurs during the load * operation */ public void load(final InputStream in) throws ConfigurationException { load(in, checkContentAndGetLocator()); }
/** * Sets system properties from a file specified using its base path and * file name. The file can either be a properties file or an XML properties * file. It is loaded, and all properties it contains are added to system * properties. * * @param basePath The base path to look for the property file. * @param fileName The name of the property file. * @throws ConfigurationException if an error occurs. * @since 1.6 */ public static void setSystemProperties(final String basePath, final String fileName) throws ConfigurationException { final FileBasedConfiguration config = fileName.endsWith(".xml") ? new XMLPropertiesConfiguration() : new PropertiesConfiguration(); final FileHandler handler = new FileHandler(config); handler.setBasePath(basePath); handler.setFileName(fileName); handler.load(); setSystemProperties(config); }
/** * 配置参数持久化<br> * 保存修改的配置到自定义配置文件({@link #USER_CONFIG_FILE}) */ public static void persistence() { try { // readConfig 方法中已经指定了文件默认编码方式为UTF-8,这里不需要再指定 FileHandler handler = new FileHandler(USER_CONFIG); handler.save(USER_CONFIG_FILE); } catch (ConfigurationException e) { throw new RuntimeException(e); } } /**
/** * Creates a new instance of {@code FileBasedBuilderParametersImpl} and * associates it with the given {@code FileHandler} object. If the handler * is <b>null</b>, a new handler instance is created. * * @param handler the associated {@code FileHandler} (can be <b>null</b>) */ public FileBasedBuilderParametersImpl(final FileHandler handler) { fileHandler = (handler != null) ? handler : new FileHandler(); }
final FileHandler fh = new FileHandler(this); fh.setFileLocator(locator); final FileLocator orgLocator = locator; try fh.load(url);
private static FileBasedConfiguration createConfiguration(Reader reader) throws ConfigurationException { if (reader == null) { throw new NullPointerException("reader: null"); } FileBasedConfigurationBuilder<FileBasedConfiguration> builder = new FileBasedConfigurationBuilder<FileBasedConfiguration>(XMLConfiguration.class) .configure(new Parameters().xml()); FileBasedConfiguration fileBasedConfiguration = builder.getConfiguration(); FileHandler handler = new FileHandler(fileBasedConfiguration); handler.load(reader); return fileBasedConfiguration; }
/** * {@inheritDoc} This implementation also creates a copy of the * {@code FileHandler}. */ @Override public FileBasedBuilderParametersImpl clone() { final FileBasedBuilderParametersImpl copy = (FileBasedBuilderParametersImpl) super.clone(); copy.fileHandler = new FileHandler(fileHandler.getContent(), fileHandler); return copy; } }
throws ConfigurationException checkContent(); final SynchronizerSupport syncSupport = fetchSynchronizerSupport(); syncSupport.lock(LockMode.WRITE); try injectFileLocator(url); if (getContent() instanceof InputStreamSupport) loadFromStreamDirectly(in); loadFromTransformedStream(in, encoding);
/** * Convenience method which saves the associated configuration. This method * expects that the managed configuration has already been created and that * a valid file location is available in the current {@code FileHandler}. * The file handler is then used to store the configuration. * * @throws ConfigurationException if an error occurs */ public void save() throws ConfigurationException { getFileHandler().save(); }
/** * Initializes the new current {@code FileHandler}. When a new result object * is created, a new {@code FileHandler} is created, too, and associated * with the result object. This new handler is passed to this method. If a * location is defined, the result object is loaded from this location. * Note: This method is called from a synchronized block. * * @param handler the new current {@code FileHandler} * @throws ConfigurationException if an error occurs */ protected void initFileHandler(final FileHandler handler) throws ConfigurationException { initEncoding(handler); if (handler.isLocationDefined()) { handler.locate(); handler.load(); } }
/** * Internal helper method for loading a file from the given input stream. * * @param in the input stream * @param locator the current {@code FileLocator} * @throws ConfigurationException if an error occurs */ private void load(final InputStream in, final FileLocator locator) throws ConfigurationException { load(in, locator.getEncoding()); }
/** * Internal helper method for loading a file from a file name. * * @param fileName the file name * @param locator the current {@code FileLocator} * @throws ConfigurationException if an error occurs */ private void load(final String fileName, final FileLocator locator) throws ConfigurationException { final FileLocator locFileName = createLocatorWithFileName(fileName, locator); final URL url = FileLocatorUtils.locateOrThrow(locFileName); load(url, locator); }
throws ConfigurationException checkContent(); final SynchronizerSupport syncSupport = fetchSynchronizerSupport(); syncSupport.lock(LockMode.WRITE); try injectFileLocator(url); Writer writer = null; saveToWriter(writer);
/** * Loads the associated file from the specified reader. * * @param in the reader * @throws ConfigurationException if an error occurs during the load * operation */ public void load(final Reader in) throws ConfigurationException { checkContent(); injectNullFileLocator(); loadFromReader(in); }
/** * Saves the associated file to the given {@code Writer}. * * @param out the {@code Writer} * @throws ConfigurationException if an error occurs during the save * operation */ public void save(final Writer out) throws ConfigurationException { checkContent(); injectNullFileLocator(); saveToWriter(out); }
@Override public FileBasedBuilderParametersImpl setFileName(final String name) { getFileHandler().setFileName(name); return this; }
/** * Checks whether a content object is available and returns the current * {@code FileLocator}. If there is no content object, an exception is * thrown. This is a typical operation to be performed before a load() or * save() operation. * * @return the current {@code FileLocator} to be used for the calling * operation */ private FileLocator checkContentAndGetLocator() throws ConfigurationException { checkContent(); return getFileLocator(); }