Properties操作如何保存到屬性文件
1、系統(tǒng)屬性
- Java中系統(tǒng)屬性就是Java的環(huán)境變量
- System.getProperties()方法會返回系統(tǒng)屬性值(Properties對象封裝)
- Properties對象的getProperty()方法返回一個String來代表系統(tǒng)屬性。
2、Properties類
- Properties類實現(xiàn)了從名字到值的映射
- Properties是存儲鍵值對的。是hashtable(map接口下的類)接口下的類。key,value都是字符串
- getProperty()方法返回一個代表該屬性值的字符串
- 使用load()或store()方法能從文件讀入屬性集或將屬性集寫入文件
- Properties在Java.util包內
3、Properties文件
存儲數(shù)據(jù)時,不僅可以將數(shù)據(jù)存儲到數(shù)據(jù)庫中(雖然這是大多數(shù)人選擇的做法)。同時,我們可以將數(shù)據(jù)保存在properties文件中。
這樣的好處是,方便、安全,因為每個注冊用戶都有一個對應的properties文件,操作的系統(tǒng)內存,而不是數(shù)據(jù)庫那樣的外存,所以會相對安全,并發(fā)性也更好。
弊端是,如果設備斷電后,數(shù)據(jù)將無法讀取甚至可能損失。----但是一般如果使用這樣方法存儲的設備都會支持備用電源,足夠時間去做好數(shù)據(jù)首尾工作。
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"); //這個屬性文件需要先創(chuàng)建(已經(jīng)存在的)
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");//這個屬性文件可以是不存在的,不存在系統(tǒng)會去自動創(chuàng)建,如果已存在系統(tǒng)會先刪除再創(chuàng)建
//將對象再保存到文件里
props.store(out, ".\\Bush.properties");
//關閉輸出流
out.close();
}
catch(IOException e)
{
System.out.println(e);
}5、一些操作
- 注冊:將用戶名、密碼存到屬性文件中
- 登錄:通過用戶名找到對應的屬性文件,如果找到,將屬性文件中的密碼和輸入的密碼比對,如果一致就成功登錄。成功后,將上一次的屬性文件的金額讀到moneybean中。
- 一系列存款取款等等操作,之后再將最后的余額set到屬性對象,最后再將屬性對象存儲到屬性文件中。
都離不開那三步。
到此這篇關于Properties操作如何保存到屬性文件的文章就介紹到這了,更多相關Properties操作保存到屬性文件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
MyBatisPlus自定義JsonTypeHandler實現(xiàn)自動轉化JSON問題
這篇文章主要介紹了MyBatisPlus自定義JsonTypeHandler實現(xiàn)自動轉化JSON問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Java GraphQL數(shù)據(jù)加載器批處理的實現(xiàn)詳解
GraphQL 數(shù)據(jù)加載器是優(yōu)化 GraphQL API 的關鍵組件,旨在解決臭名昭著的 N+1 查詢問題,在本中,我們將深入研究其批處理功能,感興趣的小伙伴可以了解下2023-12-12
Maven Plugin的@Mojo和@Execute的具體使用
本文主要介紹了Maven Plugin的@Mojo和@Execute的具體使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
Java 通過AQS實現(xiàn)數(shù)據(jù)組織
這篇文章主要介紹了通過AQS實現(xiàn)數(shù)據(jù)組織,想了解AQS的同學可以參考下2021-04-04

