package control;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;

public class TestMain {

 //根据key读取value
 public static String readValue(String filePath,String key) {
  Properties props = new Properties();
        try {
         InputStream in = new BufferedInputStream (new FileInputStream(filePath));
         props.load(in);
         String value = props.getProperty (key);
            System.out.println(key+value);
            return value;
        } catch (Exception e) {
         e.printStackTrace();
         return null;
        }
 }

 //读取properties的全部信息
    public static void readProperties(String filePath) {
     Properties props = new Properties();
        try {
         InputStream in = new BufferedInputStream (new FileInputStream(filePath));
         props.load(in);
            Enumeration en = props.propertyNames();
             while (en.hasMoreElements()) {
              String key = (String) en.nextElement();
                    String Property = props.getProperty (key);
                    System.out.println(key+Property);
                }
        } catch (Exception e) {
         e.printStackTrace();
        }
    }

    //写入properties信息
    public static void writeProperties(String filePath,String parameterName,String parameterValue) {
     Properties prop = new Properties();
     try {
      InputStream fis = new FileInputStream(filePath);
            //从输入流中读取属性列表(键和元素对)
            prop.load(fis);
            //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
            //强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
            OutputStream fos = new FileOutputStream(filePath);
            prop.setProperty(parameterName, parameterValue);
            //以适合使用 load 方法加载到 Properties 表中的格式,
            //将此 Properties 表中的属性列表(键和元素对)写入输出流
            prop.store(fos, "Update '" + parameterName + "' value");
        } catch (IOException e) {
         System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");
        }
    }

    public static void main(String[] args) {
     readValue("info.properties","url");
        writeProperties(");
        readProperties("info.properties" );
        System.out.println("OK");
    }

properties基本用法的更多相关文章

  1. 读取properties文件以及properties的用法

    package cn.util; import java.io.IOException; import java.io.InputStream; import java.util.Properties ...

  2. Properties的用法

    /** * Description:Properties是HashTable的子類,其存儲形式鍵值對. */ import java.util.*; public class PropertysTes ...

  3. Java properties文件用法

    package com.suyang.properties; import java.io.FileInputStream; import java.io.FileNotFoundException; ...

  4. JAVA中properties基本用法

    转载 源地址不详 java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式, ...

  5. java Properties的用法

    Properties是一个特殊的Map,因为和IO流牵扯到了一块…… import java.io.BufferedReader;import java.io.File;import java.io. ...

  6. 关于Properties的用法的详细解释

    如果不熟悉 java.util.Properties类,那么现在告诉您它是用来在一个文件中存储键-值对的,其中键和值是用等号分隔的.(如清单 1 所示).最近更新的java.util.Properti ...

  7. Android下用Properties保存程序配置

    读写函数分别例如以下: import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Proper ...

  8. Android通过使用Properties保存配置

    读写功能,如下面分别: import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Proper ...

  9. Java-Properties用法-入门

    对于应用程序的配置,通常的做法是将其保存在独立的配置文件中,程序启动时加载,修改时保存.Java中Properties类就提供了这样一种机制,配置项以Key-Value的数据结构存储在文本文件中,扩展 ...

随机推荐

  1. 通过EXPLAIN分析低效SQL的执行计划

    explain select * from film where rating>9\G; select_type 表示select的类型 SIMPLE 代表简单表,不用表连接或子查询 PRIMR ...

  2. JVM-类的四种载入方式

    package org.burning.sport.javase.classloader; public class ClassLoaderMain { public static void main ...

  3. 02_HTML5+CSS详解第二天

    html5大纲分析工具:https://gsnedders.html5.org/outliner/ <section> <h1>HTML部分</h1> <se ...

  4. 微信开发获取media_id错误码汇总

    微信开发遇到的错误汇总: 1. 错误代码40001 "errcode": 40001,    "errmsg": "invalid credentia ...

  5. re模块与正则表达式

    一.正则表达式概念 正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达式.常规表示法(英语:Regular Expression,在代码中常简写为regex.regexp或RE),是计算机科 ...

  6. 使用H2数据库进行单元测试

    背景 H2 数据库是一个开源的嵌入型内存数据库,采用纯Java语言实现: 程序非常小巧轻便,整个完整的Jar包也只有1.5M左右,很容易集成到项目中. 官网地址 http://www.h2databa ...

  7. 2017年总结的前端文章——CSS高级技巧汇总

    1. 页面顶部阴影 body:before{ content: ""; position: fixed; top:-10px; left:; width: 100%; height ...

  8. 主备(keepalived+haproxy)

    系统:centos6.9 mini 主机名    ip                                虚拟ip kh1     192.168.126.210 kh2     192. ...

  9. JavaBean命名规范

    ———————————————————————————————————————————————————————— 属性名/类型                    |                 ...

  10. vue2.0实现前端星星评分功能组件

    <template id="pingJia"> <div> <ul> <li :class="{li1:1,bg1:index% ...