最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)讀取和寫(xiě)入properties文件

 更新時(shí)間:2023年08月14日 14:49:42   作者:仙草不加料  
這篇文章主要介紹了Java實(shí)現(xiàn)讀取和寫(xiě)入properties文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Java讀取和寫(xiě)入properties文件

properties文件是一種屬性文件,這種文件以key=value格式存儲(chǔ)內(nèi)容。

Java中可以使用Properties類來(lái)讀取這個(gè)文件,一般properties文件作為一些參數(shù)的存儲(chǔ),使得代碼更加靈活。

這里先定義一個(gè)data.properties文件,內(nèi)容如下:

cn=12
kr=14
jp=64

既然properties是一個(gè)文件,那么我們也可以用FileInputStreram來(lái)進(jìn)行讀?。?/p>

try (BufferedInputStream bis = new BufferedInputStream(new  FileInputStream("D:\\test\\data.properties"))) {
		int data = -1;
		while((data = bis.read()) != -1) {
			System.out.print((char)data);
		}
	} catch (IOException e) {
		e.printStackTrace();
	}
    運(yùn)行結(jié)果
    // cn=12
    // kr=14
    // jp=64

但是,由于properties文件中每一行都是獨(dú)立的鍵值對(duì),這種普通讀取方式并沒(méi)有按照鍵值對(duì)的方式進(jìn)行讀取,而是逐個(gè)字節(jié)讀取,對(duì)于這種文件來(lái)講是沒(méi)有意義的。

所以就應(yīng)該使用Properties類來(lái)進(jìn)行讀取,從jdk源碼中可以看出,Properties繼承自Hashtable,因此,Properties也同樣有put和get方法,由于,properties是一個(gè)文件,Properties類提供了一個(gè)load()方法,InputStram作為數(shù)據(jù)源,傳入一個(gè)輸入流,把輸入流中的內(nèi)容傳入到內(nèi)部的key和value中,然后就能讀取到properties文件中的內(nèi)容了。

代碼如下:

// Properties格式文件的寫(xiě)入
    try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\test\\data.properties"))) {
		Properties props = new Properties();
		props.load(bis); // 將“輸入流”加載至Properties集合對(duì)象中
		// 根據(jù)key,獲取value
		System.out.println("cn");
	} catch (IOException e) {
		e.printStackTrace();
	}
    // 運(yùn)行結(jié)果
    // cn=12

同樣,在寫(xiě)properties文件時(shí),Properties類也提供了store()方法,OutputStream和String類型的注釋作為數(shù)據(jù)源,將內(nèi)部的鍵值對(duì)集合和注釋傳入到輸出流中,就可以進(jìn)行寫(xiě)入操作了。

代碼如下:

// Properties格式文件的寫(xiě)入
	try {
		Properties props = new Properties();
		props.put("F1", "2314");
		props.put("F2", "2341");
		props.put("F3", "5322");
		props.put("F4", "4316");
		// 使用“輸出流”,將Properties集合中的KV鍵值對(duì),寫(xiě)入*.properties文件
		try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\test\\demo.properties"))){
			props.store(bos, "just do IT");
		}
	} catch (IOException e) {
		e.printStackTrace();
	}

可以看到,demo.properties文件已經(jīng)被創(chuàng)建出來(lái)了,內(nèi)容包括注釋、創(chuàng)建時(shí)間和每對(duì)鍵值對(duì) 。

Java讀寫(xiě)properties文件(java.util.Properties)

Java對(duì)于properties文件的讀寫(xiě)可以說(shuō)是最簡(jiǎn)單的一個(gè)讀取、寫(xiě)入配置文件的方法了,在properties文件中,數(shù)據(jù)是用類似于鍵值對(duì)的存儲(chǔ)方式進(jìn)行存儲(chǔ)的。

下面就是一個(gè)簡(jiǎn)單的properties文件:

username=xm99
password=1234567

沒(méi)錯(cuò),就是這么簡(jiǎn)單的方式。

那么我們想要對(duì)properties文件進(jìn)行操作的時(shí)候應(yīng)該如何操作呢?

  • 生成一個(gè)properties對(duì)象
  • 使用properties對(duì)象的 String name = p.getProperty("valueName"); 方法獲得配置的值
  • 使用properties對(duì)象的 setProperty(String key,String value) 方法在配置文件中增加配置信息。之后使用 store() 方法進(jìn)行保存
  • 關(guān)閉輸入流/輸出流對(duì)象

寫(xiě)入配置文件代碼實(shí)例

package proTest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class proSavaTest {
    public static void main(String[] args) throws IOException {
        FileOutputStream out = new FileOutputStream("D:\\demo\\javaBase\\src\\proTest\\pro.properties");
        Properties pro = new Properties();
        pro.setProperty("username","xm99");
        pro.setProperty("passwd","12345");
        pro.store(out,"acb");
    }
}

讀取配置文件示例

package proTest;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class proTest {
    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream("D:\\demo\\javaBase\\src\\proTest\\pro.properties");
        Properties pro = new Properties();
        pro.load(in);
        String username = pro.getProperty("username");
        String passwd = pro.getProperty("passwd");
        System.out.println("username="+username);
        System.out.println("passwd="+passwd);
        in.close();
    }
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

濮阳县| 类乌齐县| 元谋县| 沙湾县| 同江市| 顺昌县| 仁布县| 嵊泗县| 子洲县| 南汇区| 庆云县| 阿拉善盟| 开封县| 南木林县| 桦川县| 克什克腾旗| 衡水市| 仪陇县| 潼关县| 乌恰县| 万全县| 周至县| 兴安县| 通化县| 故城县| 大兴区| 江陵县| 迁安市| 丹江口市| 昭平县| 菏泽市| 康定县| 望都县| 饶平县| 固镇县| 河北省| 万盛区| 陆河县| 青神县| 拉孜县| 天祝|