java8.5中加入背景_8.5_java学习_对配置文件操作

/*

Properties 是hashtable的子类

也就是说它具备map集合的特点,而且它里面存储的键值都是字符串。

是集合中和IO技术相结合的集合容器。

该对象的特点: 可以用于键值对的配置文件。

*/

import java.io.*;

import java.util.*;

class Properties1

{

public static void main(String[] args) throws IOException

{

loadDemo();

//method_1();

//setAndGet();

}

//演示:如何将流中的数据存储到集合中。

//想要将info.txt中的数据存到集合中进行操作.

/*

1,用一个流和info.txt文件关联。

2,读取一行数据,将该行数据用“=”进行切割。

3,等号左边作为键。右边作为值。存入到Properties中

*/

public static void loadDemo() throws IOException

{

Properties prop = new Properties();

FileInputStream fis = new FileInputStream("info.txt");

FileOutputStream fos = new FileOutputStream("info.txt");

//将流中的数据加载进集合。

prop.load(fis);

prop.setProperty("lisi","56");

prop.store(fos,"haha");

//System.out.print(prop);

prop.list(System.out);

fos.close();

fis.close();

}

public static void method_1()throws IOException

{

BufferedReader buff = new BufferedReader(new FileReader("info.txt"));

String line = null;

Properties prop = new Properties();

while((line=buff.readLine())!= null)

{

String[] arr = line.split("=");

prop.setProperty(arr[0],arr[1]);

//System.out.println(arr[0]+"......"+arr[1]);

}

System.out.println(prop);

buff.close();

}

//设置和获取元素。

public static void setAndGet()

{

Properties prop = new Properties();

prop.setProperty("zhan san","30");

prop.setProperty("li si","23");

//System.out.println(prop);

String value = prop.getProperty("li si");

System.out.println(value);

Set names = prop.stringPropertyNames();

for(String s : names)

{

System.out.println(s);

}

}

}

/* Properties 是hashtable的子类 也就是说它具备map集合的特点,而且它里面存储的键值都是字符串。 是集合中和IO技术相结合的集合容器。 该对象的特点: 可以用于键值对的配置文件。 */ import java.io.*; import java.util.*; class Properties1 { public static void main(String[] args) throws IOException { loadDemo(); //method_1(); //setAndGet(); } //演示:如何将流中的数据存储到集合中。 //想要将info.txt中的数据存到集合中进行操作. /* 1,用一个流和info.txt文件关联。 2,读取一行数据,将该行数据用“=”进行切割。 3,等号左边作为键。右边作为值。存入到Properties中 */ public static void loadDemo() throws IOException { Properties prop = new Properties(); FileInputStream fis = new FileInputStream("info.txt"); FileOutputStream fos = new FileOutputStream("info.txt"); //将流中的数据加载进集合。 prop.load(fis); prop.setProperty("lisi","56"); prop.store(fos,"haha"); //System.out.print(prop); prop.list(System.out); fos.close(); fis.close(); } public static void method_1()throws IOException { BufferedReader buff = new BufferedReader(new FileReader("info.txt")); String line = null; Properties prop = new Properties(); while((line=buff.readLine())!= null) { String[] arr = line.split("="); prop.setProperty(arr[0],arr[1]); //System.out.println(arr[0]+"......"+arr[1]); } System.out.println(prop); buff.close(); } //设置和获取元素。 public static void setAndGet() { Properties prop = new Properties(); prop.setProperty("zhan san","30"); prop.setProperty("li si","23"); //System.out.println(prop); String value = prop.getProperty("li si"); System.out.println(value); Set names = prop.stringPropertyNames(); for(String s : names) { System.out.println(s); } } }
经验分享 程序员 微信小程序 职场和发展