/**
* 实现对Java配置文件Properties的读取、写入与更新操作
*/
package test; import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties; /**
* @author
* @song
*/
public class demo { //属性文件的路径
static String profilepath="WebRoot/WEB-INF/admin/admin.properties";
/**
* 采用静态方法
*/
private static Properties props = new Properties();
static {
try {
props.load(new FileInputStream(profilepath));
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(-1);
} catch (IOException e) {
System.exit(-1);
}
} /**
* 读取属性文件中相应键的值
* @param key
* 主键
* @return String
*/
public static String getKeyValue(String key) {
return props.getProperty(key);
} /**
* 根据主键key读取主键的值value
* @param filePath 属性文件路径
* @param key 键名
*/
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);
if(value.equals("123456")){
System.out.println("登录成功!");
}else {
System.out.println("登录失败!");
}
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
} /**
* 插入一对properties信息(主键及其键值)
* @param keyname 键名
* @param keyvalue 键值
*/
public static void writeProperties(String keyname,String keyvalue) {
try {
// 调用 Hashtable 的方法 put,使用 getProperty 方法提供并行性。
// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream fos = new FileOutputStream(profilepath);
props.setProperty(keyname, keyvalue);
// 以适合使用 load 方法加载到 Properties 表中的格式,
// 将此 Properties 表中的属性列表(键和元素对)写入输出流
props.store(fos, "Update '" + keyname + "' value");
} catch (IOException e) {
System.err.println("属性文件更新错误");
}
} /**
* 更新properties文件的键值对
* @param keyname 键名
* @param keyvalue 键值
*/
public void updateProperties(String keyname,String keyvalue) {
try {
props.load(new FileInputStream(profilepath));
// 调用 Hashtable 的方法 put,使用 getProperty 方法提供并行性。
// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream fos = new FileOutputStream(profilepath);
props.setProperty(keyname, keyvalue);
// 以适合使用 load 方法加载到 Properties 表中的格式,
// 将此 Properties 表中的属性列表(键和元素对)写入输出流
props.store(fos, "Update '" + keyname + "' value");
} catch (IOException e) {
System.err.println("属性文件更新错误");
}
}
//测试代码
public static void main(String[] args) {
readValue("WebRoot/WEB-INF/admin/admin.properties", "zs");
//writeProperties("ls", "123456,1");
System.out.println("操作完成");
}
}

  

实现对Java配置文件Properties的读取、写入与更新操作的更多相关文章

  1. Java配置文件Properties的读取、写入与更新操作

    /** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...

  2. 对Java配置文件Properties的读取、写入与更新操作

    http://breezylee.iteye.com/blog/1340868 对Java配置文件Properties的读取.写入与更新操作 博客分类: javase properties  对Jav ...

  3. 关于Java配置文件properties的学习

    在Java早期的开发中,常用*.properties文件存储一些配置信息.其文件中的信息主要是以key=value的方式进行存储,在早期受到广泛的应用.而后随着xml使用的广泛,其位置渐渐被取代,不过 ...

  4. 读取java配置文件properties

    java项目里很多参数都是写在配置文件properties上,如果需要读取的话,可以使用jdk里提供的Properties类进行处理. 具体写法如下: public class PropertiesC ...

  5. 【Properties文件】Java使用Properties来读取配置文件

    配置文件位置及内容 执行结果 程序代码 package Utils.ConfigFile;   import java.io.BufferedInputStream; import java.io.B ...

  6. Java的properties文件读取和属性修改

    package Test; import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileO ...

  7. java配置文件properties,yml,一般文件

    JAVA编写配置文件的几种方式: JAVA配置文件,一般都放在resource目录下,无论是下面提到的properties.yml还是普通的txt等文件. 在打成jar包之后,只需要jar包程序就可运 ...

  8. Java之properties文件读取

    1.工程结构 2.ConfigFileTest.java package com.configfile; import java.io.IOException; import java.io.Inpu ...

  9. Log4j实现对Java日志的配置全攻略

    1. 配置文件 Log4J配置文件的基本格式如下: #配置根Logger log4j.rootLogger = [ level ] , appenderName1 , appenderName2 , ...

随机推荐

  1. activiti 任务监听参数的配置

    public class TaskListenersImp implements TaskListener { private Expression level; private Expression ...

  2. eclipse+gradle

    一.gradle 下载与安装 下载地址:http://services.gradle.org/distributions/ 下载后,放到:D:\gradle-4.6 在系统环境变量path,中加:D: ...

  3. java web中分层MVC的意义

    在web编程中,由于高内聚.低耦合的特点,需要将多个类实现多层,大致有以下几层:①entity,实体类,如user,role等,这些类里边包含了私有属性和公共的get.set方法这和数据库中的表相对应 ...

  4. java注解篇

    @SuppressWarnings注解 该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默. 允许您选择性地取消特定代码段(即,类或方法)中的警告.其中的想法是当您看到警告 ...

  5. declaration may not appear after executable statement in block

    keil 编译时出现 declaration may not appear after executable statement in block,找到keil工程对应的函数 定义的地方出现在了赋值的 ...

  6. sudo 取消密码

    通常我们并不以root身份登录,但是当我们执行某些命令 (command)时需要用到root权限,我们通常都是用"sudo command"来执行command.由于使用Ubunt ...

  7. vs2017使用问题

    最近安装了新版本的Visual studio  2017,但是在使用的过程中遇到了这样一个问题.刚启动电脑后,打开vs2017是可以打开的,但是当关掉之后再打开就打不开了,但是任务管理器看可以看到有一 ...

  8. Verilog语言

    for循环应用 1.复位寄存器组 例如有32个寄存器,需要异步复位 always@(posedge clk or negedge rst_n) begin if (rst_n == 1'b0) beg ...

  9. springboot学习一:快速搭建springboot项目

    1.idea创建springboot工程 JDK选择1.8以上的版本 选择springboot的版本和添加配置项 新建一个HelloController,测试 访问 http://localhost: ...

  10. redis 安装和配置

    准备条件:1>确保所安装的环境能够访问网络,2>环境中拥有gcc\g++.make.tar等工具3>以root身份登录安装过程:1>输入命令:wget http://downl ...