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