Properties操作如何保存到屬性文件
1、系統(tǒng)屬性
- Java中系統(tǒng)屬性就是Java的環(huán)境變量
- System.getProperties()方法會(huì)返回系統(tǒng)屬性值(Properties對象封裝)
- Properties對象的getProperty()方法返回一個(gè)String來代表系統(tǒng)屬性。
2、Properties類
- Properties類實(shí)現(xiàn)了從名字到值的映射
- Properties是存儲鍵值對的。是hashtable(map接口下的類)接口下的類。key,value都是字符串
- getProperty()方法返回一個(gè)代表該屬性值的字符串
- 使用load()或store()方法能從文件讀入屬性集或?qū)傩约瘜懭胛募?/li>
- Properties在Java.util包內(nèi)
3、Properties文件
存儲數(shù)據(jù)時(shí),不僅可以將數(shù)據(jù)存儲到數(shù)據(jù)庫中(雖然這是大多數(shù)人選擇的做法)。同時(shí),我們可以將數(shù)據(jù)保存在properties文件中。
這樣的好處是,方便、安全,因?yàn)槊總€(gè)注冊用戶都有一個(gè)對應(yīng)的properties文件,操作的系統(tǒng)內(nèi)存,而不是數(shù)據(jù)庫那樣的外存,所以會(huì)相對安全,并發(fā)性也更好。
弊端是,如果設(shè)備斷電后,數(shù)據(jù)將無法讀取甚至可能損失。----但是一般如果使用這樣方法存儲的設(shè)備都會(huì)支持備用電源,足夠時(shí)間去做好數(shù)據(jù)首尾工作。
4、Properties文件的讀寫操作
- 首先,我們并不能直接對properties文件進(jìn)行操作,而是必須先將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一個(gè)properties對象
Properties props = new Properties();
//新建一個(gè)輸入流(屬性文件),讀取到properties對象里
File f=new File(".\\OracleSetup.properties"); //這個(gè)屬性文件需要先創(chuàng)建(已經(jīng)存在的)
FileInputStream in = new FileInputStream(f);
//讀入對象
props.load(in);
//關(guān)閉流
in.close();對屬性對象操作,將對象保存到屬性文件
//對屬性對象進(jìn)行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");//這個(gè)屬性文件可以是不存在的,不存在系統(tǒng)會(huì)去自動(dòng)創(chuàng)建,如果已存在系統(tǒng)會(huì)先刪除再創(chuàng)建
//將對象再保存到文件里
props.store(out, ".\\Bush.properties");
//關(guān)閉輸出流
out.close();
}
catch(IOException e)
{
System.out.println(e);
}5、一些操作
- 注冊:將用戶名、密碼存到屬性文件中
- 登錄:通過用戶名找到對應(yīng)的屬性文件,如果找到,將屬性文件中的密碼和輸入的密碼比對,如果一致就成功登錄。成功后,將上一次的屬性文件的金額讀到moneybean中。
- 一系列存款取款等等操作,之后再將最后的余額set到屬性對象,最后再將屬性對象存儲到屬性文件中。
都離不開那三步。
到此這篇關(guān)于Properties操作如何保存到屬性文件的文章就介紹到這了,更多相關(guān)Properties操作保存到屬性文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA下創(chuàng)建SpringBoot+MyBatis+MySql項(xiàng)目實(shí)現(xiàn)動(dòng)態(tài)登錄與注冊功能
這篇文章主要介紹了IDEA下創(chuàng)建SpringBoot+MyBatis+MySql項(xiàng)目實(shí)現(xiàn)動(dòng)態(tài)登錄與注冊功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
JAVASE系統(tǒng)實(shí)現(xiàn)抽卡功能
這篇文章主要為大家詳細(xì)介紹了JAVASE系統(tǒng)實(shí)現(xiàn)抽卡功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
Java編程實(shí)現(xiàn)生成給定范圍內(nèi)不重復(fù)隨機(jī)數(shù)的方法小結(jié)
這篇文章主要介紹了Java編程實(shí)現(xiàn)生成給定范圍內(nèi)不重復(fù)隨機(jī)數(shù)的方法,結(jié)合實(shí)例形式總結(jié)分析了java基于數(shù)學(xué)運(yùn)算與判斷實(shí)現(xiàn)不重復(fù)隨機(jī)數(shù)的生成功能,需要的朋友可以參考下2017-07-07
Spring5中SpringWebContext方法過時(shí)的解決方案
這篇文章主要介紹了Spring5中SpringWebContext方法過時(shí)的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
SpringBoot+Mybatis Plus導(dǎo)致PageHelper失效的解決方法
在Springboot項(xiàng)目中使用分頁插件的時(shí)候,發(fā)現(xiàn)PageHelper插件失效了 ,本文主要介紹了SpringBoot+Mybatis Plus導(dǎo)致PageHelper失效的解決方法,感興趣的可以了解一下2024-07-07
SpringBoot整合mybatis-plus進(jìn)階詳細(xì)教程
本文主要對mybatis-plus的條件構(gòu)造器、AR模式、插件、逆向工程、自定義全局操作、公共字段自動(dòng)填充等知識點(diǎn)進(jìn)行講解,需要的朋友參考下吧2021-09-09

