private static Properties readPropertiesFromFile (File propertiesFile) throws IOException { InputStream stream = null; try { stream = new FileInputStream(propertiesFile); Properties properties = new Properties(); properties.load(stream); return properties; } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
/** * Edits the specified property within the given property file * * @param filePath path to the property file * @param propertyName property name to edit * @param propertyValue new property value * @param append whether or not to append the given property value. If true appends the given * property value the current value. If false, replaces the current property value with the * given property value */ protected static void editPropertyFile(String filePath, String propertyName, String propertyValue, boolean append) throws Exception { FileInputStream input = new FileInputStream(filePath); Properties properties = new Properties(); properties.load(input); String val; if (append) { val = properties.getProperty(propertyName) + propertyValue; } else { val = propertyValue; } properties.setProperty(propertyName, val); properties.store(new FileOutputStream(filePath), null); logger.info("Modified container Property file " + filePath); }
private static Properties createSampleProperties() { Properties properties = new Properties(); properties.put("com.example.service.One", "service"); properties.put("com.example.service.sub.Two", "service"); properties.put("com.example.service.Three", "service"); properties.put("com.example.domain.Four", "entity"); return properties; }
private static String versionString() { try { Properties prop = new Properties(); InputStream in = Main.class.getResourceAsStream("/okcurl-version.properties"); prop.load(in); in.close(); return prop.getProperty("version"); } catch (IOException e) { throw new AssertionError("Could not load okcurl-version.properties."); } }
Properties properties = new Properties(); try { properties.load(new FileInputStream("path/filename")); } catch (IOException e) { ... }
@Override public Properties convert(String source) { try { Properties props = new Properties(); // Must use the ISO-8859-1 encoding because Properties.load(stream) expects it. props.load(new ByteArrayInputStream(source.getBytes(StandardCharsets.ISO_8859_1))); return props; } catch (Exception ex) { // Should never happen. throw new IllegalArgumentException("Failed to parse [" + source + "] into Properties", ex); } }
System.exit(0); props.setProperty(Client.DB_PROPERTY, args[argindex]); argindex++; } else if (args[argindex].compareTo("-P") == 0) { argindex++; Properties myfileprops = new Properties(); try { myfileprops.load(new FileInputStream(propfile)); } catch (IOException e) { System.out.println(e.getMessage()); for (Enumeration e = myfileprops.propertyNames(); e.hasMoreElements();) { String prop = (String) e.nextElement(); fileprops.setProperty(prop, myfileprops.getProperty(prop)); props.put(name, value); argindex++; } else if (args[argindex].compareTo("-table") == 0) { System.exit(0); props.put(CoreWorkload.TABLENAME_PROPERTY, args[argindex]);
Properties properties = new Properties(); FileInputStream input = new FileInputStream(fileName); try { properties.load(input); } finally { input.close(); Enumeration<java.net.URL> urls = ClassHelper.getClassLoader().getResources(fileName); list = new ArrayList<java.net.URL>(); while (urls.hasMoreElements()) { list.add(urls.nextElement()); properties.load(ClassHelper.getClassLoader().getResourceAsStream(fileName)); } catch (Throwable e) { logger.warn("Failed to load " + fileName + " file from " + fileName + "(ignore this file): " + e.getMessage(), e); Properties p = new Properties(); InputStream input = url.openStream(); if (input != null) { try { p.load(input); properties.putAll(p); } finally { try { input.close(); } catch (Throwable t) {
Properties props = new Properties(); try { Enumeration<URL> resources = classLoader.getResources("META-INF/io.netty.versions.properties"); while (resources.hasMoreElements()) { URL url = resources.nextElement(); InputStream in = url.openStream(); try { props.load(in); } finally { try { in.close(); } catch (Exception ignore) { for (Object o: props.keySet()) { String k = (String) o; if (!props.containsKey(artifactId + PROP_VERSION) || !props.containsKey(artifactId + PROP_BUILD_DATE) || !props.containsKey(artifactId + PROP_COMMIT_DATE) || !props.containsKey(artifactId + PROP_SHORT_COMMIT_HASH) || !props.containsKey(artifactId + PROP_LONG_COMMIT_HASH) || !props.containsKey(artifactId + PROP_REPO_STATUS)) { continue; new Version( artifactId, props.getProperty(artifactId + PROP_VERSION), parseIso8601(props.getProperty(artifactId + PROP_BUILD_DATE)), parseIso8601(props.getProperty(artifactId + PROP_COMMIT_DATE)),
Properties entityReferences = new Properties(); entityReferences.load(is); is.close(); Enumeration<?> keys = entityReferences.propertyNames(); while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); int referredChar = Integer.parseInt(key); Assert.isTrue((referredChar < 1000 || (referredChar >= 8000 && referredChar < 10000)), () -> "Invalid reference to special HTML entity: " + referredChar); int index = (referredChar < 1000 ? referredChar : referredChar - 7000); String reference = entityReferences.getProperty(key); this.characterToEntityReferenceMap[index] = REFERENCE_START + reference + REFERENCE_END; this.entityReferenceToCharacterMap.put(reference, (char) referredChar);
/** * This method reads in properties listed in a file in the format prop=value, one property per line. * Although {@code Properties.load(InputStream)} exists, I implemented this method to trim the lines, * something not implemented in the {@code load()} method. * * @param filename A properties file to read * @return The corresponding Properties object */ public static Properties propFileToProperties(String filename) { try { InputStream is = new BufferedInputStream(new FileInputStream(filename)); Properties result = new Properties(); result.load(is); // trim all values for (String propKey : result.stringPropertyNames()){ String newVal = result.getProperty(propKey); result.setProperty(propKey,newVal.trim()); } is.close(); return result; } catch (IOException e) { throw new RuntimeIOException("propFileToProperties could not read properties file: " + filename, e); } }
private static void loadFilterConfig(Properties filterProperties, ClassLoader classLoader) throws IOException { if (classLoader == null) { return; } for (Enumeration<URL> e = classLoader.getResources("META-INF/druid-filter.properties"); e.hasMoreElements();) { URL url = e.nextElement(); Properties property = new Properties(); InputStream is = null; try { is = url.openStream(); property.load(is); } finally { JdbcUtils.close(is); } filterProperties.putAll(property); } }
Properties props = new Properties(); while (urls.hasMoreElements()) { URL url = urls.nextElement(); URLConnection con = url.openConnection(); ResourceUtils.useCachesIfNecessary(con); InputStream is = con.getInputStream(); try { if (resourceName.endsWith(XML_FILE_EXTENSION)) { props.loadFromXML(is); props.load(is); is.close();
FileInputStream in = new FileInputStream("First.properties"); Properties props = new Properties(); props.load(in); in.close(); FileOutputStream out = new FileOutputStream("First.properties"); props.setProperty("country", "america"); props.store(out, null); out.close();
/** * Copy the configured output {@link Properties}, if any, into the * {@link Transformer#setOutputProperty output property set} of the supplied * {@link Transformer}. * @param transformer the target transformer */ protected final void copyOutputProperties(Transformer transformer) { if (this.outputProperties != null) { Enumeration<?> en = this.outputProperties.propertyNames(); while (en.hasMoreElements()) { String name = (String) en.nextElement(); transformer.setOutputProperty(name, this.outputProperties.getProperty(name)); } } }
private static Properties readPropertyFile() { Properties properties = new Properties(); properties.load(inputStream); } catch (IOException e) { e.printStackTrace(); properties.load(new FileInputStream(propertyFile())); } catch (FileNotFoundException e) { if (MockServerLogger.MOCK_SERVER_LOGGER != null) { if (!properties.isEmpty()) { Enumeration<?> propertyNames = properties.propertyNames(); while (propertyNames.hasMoreElements()) { String propertyName = String.valueOf(propertyNames.nextElement()); propertiesLogDump.append("\t").append(propertyName).append(" = ").append(properties.getProperty(propertyName)).append(NEW_LINE);
@DataProvider(name = "provider") public Object[][] createData() throws FileNotFoundException, IOException { Properties p = new Properties(); List<Object> vResult = new ArrayList<>(); p.load(new FileInputStream(new File("c:/t/data.properties"))); for (Enumeration e = p.keys(); e.hasMoreElements(); ) { vResult.add(e.nextElement()); } Object[][] result = new Object[vResult.size()][1]; for (int i = 0; i < result.length; i++) { result[i] = new Object[] { vResult.get(i) }; } return result; }
/** * 把字符串键值对的map写入文件 */ public static void writeMap(String filePath, Map<String, String> map, boolean append, String comment) { if (map == null || map.size() == 0 || StringUtils.isEmpty(filePath)) { return; } FileInputStream fis = null; FileOutputStream fos = null; File f = new File(filePath); try { if (!f.exists() || !f.isFile()) { f.createNewFile(); } Properties p = new Properties(); if (append) { fis = new FileInputStream(f); p.load(fis);// 先读取文件,再把键值对追加到后面 } p.putAll(map); fos = new FileOutputStream(f); p.store(fos, comment); } catch (Exception e) { LogUtils.e(e); } finally { IOUtils.close(fis); IOUtils.close(fos); } }
public static void main(String[] args) throws Exception { Properties props = StringUtils.argsToProperties(args); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); String file = props.getProperty("file"); String loadFile = props.getProperty("loadFile"); if (loadFile != null && ! loadFile.isEmpty()) { CustomAnnotationSerializer ser = new CustomAnnotationSerializer(false, false); InputStream is = new FileInputStream(loadFile); Pair<Annotation, InputStream> pair = ser.read(is); pair.second.close(); Annotation anno = pair.first; System.out.println(anno.toShorterString(StringUtils.EMPTY_STRING_ARRAY)); is.close(); } else if (file != null && ! file.equals("")) { String text = edu.stanford.nlp.io.IOUtils.slurpFile(file); Annotation doc = new Annotation(text); pipeline.annotate(doc); CustomAnnotationSerializer ser = new CustomAnnotationSerializer(false, false); PrintStream os = new PrintStream(new FileOutputStream(file + ".ser")); ser.write(doc, os).close(); log.info("Serialized annotation saved in " + file + ".ser"); } else { log.info("usage: CustomAnnotationSerializer [-file file] [-loadFile file]"); } }
/** * Populate the available server public keys into a local static HashMap. This method is not * thread safe. */ public static void initCertsMap(Properties props) throws Exception { certificateMap = new HashMap(); certificateFilePath = props.getProperty(PUBLIC_KEY_FILE_PROP); if (certificateFilePath != null && certificateFilePath.length() > 0) { KeyStore ks = KeyStore.getInstance("JKS"); String keyStorePass = props.getProperty(PUBLIC_KEY_PASSWD_PROP); char[] passPhrase = (keyStorePass != null ? keyStorePass.toCharArray() : null); FileInputStream keystorefile = new FileInputStream(certificateFilePath); try { ks.load(keystorefile, passPhrase); } finally { keystorefile.close(); } Enumeration aliases = ks.aliases(); while (aliases.hasMoreElements()) { String alias = (String) aliases.nextElement(); Certificate cert = ks.getCertificate(alias); if (cert instanceof X509Certificate) { String subject = ((X509Certificate) cert).getSubjectDN().getName(); certificateMap.put(subject, cert); } } } }