Properties操作保存到属性文件

1、系统属性

  • Java中系统属性就是Java的环境变量
  • System.getProperties()方法会返回系统属性值(Properties对象封装)
  • Properties对象的getProperty()方法返回一个String来代表系统属性。

2、Properties类

  • Properties类实现了从名字到值的映射
  • Properties是存储键值对的。是hashtable(map接口下的类)接口下的类。key,value都是字符串
  • getProperty()方法返回一个代表该属性值的字符串
  • 使用load()或store()方法能从文件读入属性集或将属性集写入文件
  • Properties在Java.util包内

3、Properties文件

存储数据时,不仅可以将数据存储到数据库中(虽然这是大多数人选择的做法)。同时,我们可以将数据保存在properties文件中。

这样的好处是,方便、安全,因为每个注册用户都有一个对应的properties文件,操作的系统内存,而不是数据库那样的外存,所以会相对安全,并发性也更好。

弊端是,如果设备断电后,数据将无法读取甚至可能损失。----但是一般如果使用这样方法存储的设备都会支持备用电源,足够时间去做好数据首尾工作。

4、Properties文件的读写操作

  • 首先,我们并不能直接对properties文件进行操作,而是必须先将properties文件读到propertires对象上,然后通过对象去set或者get,最后再将对象保存到属性文件。这样就是读写操作的过程

只要涉及Properties的操作,大体可以分为以下三步:

演示代码


import java.util.*;
import java.io.*;

public class ReadPro
{   static Properties props;
	private String oracle_url,oracle_name,oracle_user,oracle_pwd;
	private String file_path,virtual_path;
	
	static
	{
		try
		{
			props = new Properties();
			File f=new File(".\\OracleSetup.properties");
			FileInputStream in = new FileInputStream(f);
			props.load(in);
			in.close();
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
	
	}
	
	
	
	public ReadPro()
	{
			this.oracle_url   = this.props.getProperty("oracle_url");
			this.oracle_name  = this.props.getProperty("oracle_name");
			this.oracle_user  = this.props.getProperty("oracle_user");
			this.oracle_pwd   = this.props.getProperty("oracle_pwd");
			this.file_path    = this.props.getProperty("file_path");
			this.virtual_path = this.props.getProperty("virtual_path");
			
			
		 	
		}
		
	public void loadproper()
	{    props.setProperty("oracle_user", "bush");
		 props.setProperty("password", "1234");
		 props.setProperty("age", "19");
         props.setProperty("sex", "female");
         props.setProperty("score", "99.9");
			try{
			FileOutputStream out = new 
			     FileOutputStream(".\\Bush.properties");
		    props.store(out, ".\\Bush.properties");
			out.close();
		   }
		  catch(IOException e)
		{
			System.out.println(e);
		}
	 }

	public String getOracle_url()
	{
		return oracle_url;
	}
	public String getOracle_name()
	{
		return oracle_name;
	}
	public String getOracle_user()
	{
		return oracle_user;
	}
	public String getOracle_pwd()
	{
		return oracle_pwd;
	}
	public String getFile_path()
	{
		return file_path;
	} 
	public String getVirtual_path()
	{
		return virtual_path;
	}
	
	public static void main(String args[])
	{
		
		ReadPro rp = new ReadPro();
		System.out.println("oracle_user="+rp.getOracle_user());
	    System.out.println("oracle_name = " + rp.getOracle_name());
		System.out.println("oracle_pwd = " +rp.getOracle_pwd());
		System.out.println("file_path = " +rp.getFile_path());
	    System.err.println("file_path = " +rp.getFile_path()); 
	    System.out.println("virtual_path = " +rp.getVirtual_path());
	   
		//存储Properties 到属性文件
	    rp.loadproper();
	}	
}	
  • 将属性文件读到属性对象上
			//new一个properties对象
			Properties props = new Properties();
			//新建一个输入流(属性文件),读取到properties对象里
			File f=new File(".\\OracleSetup.properties"); //这个属性文件需要先创建(已经存在的)
			FileInputStream in = new FileInputStream(f);
			//读入对象
			props.load(in);
			//关闭流
			in.close();

  • 对属性对象操作,将对象保存到属性文件
 		
		//对属性对象进行set操作。以键值对的方式set,前面是字段key,后面是值value
		props.setProperty("oracle_user", "bush");
		 props.setProperty("password", "1234");
		 props.setProperty("age", "19");
         props.setProperty("sex", "female");
         props.setProperty("score", "99.9");
			try{
			FileOutputStream out = new 
			     FileOutputStream(".\\Bush.properties");//这个属性文件可以是不存在的,不存在系统会去自动创建,如果已存在系统会先删除再创建
              //将对象再保存到文件里
		    props.store(out, ".\\Bush.properties");
              //关闭输出流
			out.close();
		   }
		  catch(IOException e)
		{
			System.out.println(e);
		}

5、一些操作

  • 注册:将用户名、密码存到属性文件中
  • 登录:通过用户名找到对应的属性文件,如果找到,将属性文件中的密码和输入的密码比对,如果一致就成功登录。成功后,将上一次的属性文件的金额读到moneybean中。
  • 一系列存款取款等等操作,之后再将最后的余额set到属性对象,最后再将属性对象存储到属性文件中。

都离不开那三步。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zero摄氏度

感谢鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值