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. junit断言总结

    我们平时编写自己的测试类,如果没有断言,那么就没写测试的必要了. JUnit框架用一组assert方法封装了最常见的测试任务.这些assert方法可以极大地简化单元测试的编写. Assert类包含了一 ...

  2. SQL Server中的变更捕获技术--简单部署

    ------准备------ CREATE DATABASE db_test_cdc ,) ,name )); INSERT INTO t1(name)VALUES('test') ------开始- ...

  3. 关于OMAPL138烧写程序的说明

    相信很多朋友在用CCS调试OMAPL138开发板的时候,肯定遇到了许许多多的问题: 例如: 1.CCS安装不完整,导致有些功能无法使用 2.ARM端没有加载gel文件,使得程序无法被唤醒 3.ccxm ...

  4. 07_Python变量内存地址、小数据池

    一.变量在内存中的地址 变量:用来标识(identify)一块内存区域.为了方便表示内存,我们操作变量实质上是在操作变量指向的那块内存单元.编译器负责分配.我们可以使用Python内建函数id()来获 ...

  5. python unicode 字节串转成中文问题

    字符串:s = r"\u65b0\u6d6a\u5fae\u535a\u6ce8\u518c" 转换为中文:s = s.decode("unicode_escape&qu ...

  6. NoSQL在大数据中的应用

    一.序言 NoSQL是Not Only SQL的缩写,而不是Not SQL,指的是非关系型的数据库,它不一定遵循传统数据库的一些基本要求,比如说遵循SQL标准.ACID属性.表结构等等.相比传统数据库 ...

  7. My Calendar III

    class MyCalendarThree(object): """ Implement a MyCalendarThree class to store your ev ...

  8. NemaStudio船舶模拟软件下载及破解

    不啰嗦,上链接: https://files.cnblogs.com/files/lizhijian/NameStudio%E7%A0%B4%E8%A7%A3.zip 感谢阅读,希望可以帮到你.

  9. c# 简单实现 插件模型 反射方式

    利用反射方式实现插件模型,wpf控件作为插件,然后用另外的窗体加载. 首先定义插件接口: public interface IUserControlLevel1 { string PluginName ...

  10. 【java学习】spring mvc 公共dao的实现,定义基本的增删改查

    接口类: package com.blog.db.dao; import com.blog.util.Pagination; import java.util.List; public interfa ...