Java属性类:Properties的常用方法
来源:互联网 发布:支付宝知托付 广告 编辑:程序博客网 时间:2024/06/05 02:57
Properties类本身是Hashtable类的子类,也是按照key-value的形式存放数据的.
设置和取得属性:
public class PropertiesDemo01{public static void main(String args[]){Properties pro = new Properties() ;// 创建Properties对象pro.setProperty("BJ","BeiJing") ;// 设置属性pro.setProperty("TJ","TianJin") ;pro.setProperty("NJ","NanJing") ;System.out.println("1、BJ属性存在:" + pro.getProperty("BJ")) ;System.out.println("2、SC属性不存在:" + pro.getProperty("SC")) ;System.out.println("3、SC属性不存在,同时设置显示的默认值:" + pro.getProperty("SC","没有发现")) ;}};
将属性保存到普通的属性文件中:
public class PropertiesDemo02{public static void main(String args[]){Properties pro = new Properties() ;// 创建Properties对象pro.setProperty("BJ","BeiJing") ;// 设置属性pro.setProperty("TJ","TianJin") ;pro.setProperty("NJ","NanJing") ;File file = new File("D:" + File.separator + "area.properteis") ;// 指定要操作的文件try{pro.store(new FileOutputStream(file),"Area Info") ;// 保存属性到普通文件}catch(FileNotFoundException e){e.printStackTrace() ;}catch(IOException e){e.printStackTrace() ;}}};
从属性文件中读取内容:
public class PropertiesDemo03{public static void main(String args[]){Properties pro = new Properties() ;// 创建Properties对象File file = new File("D:" + File.separator + "area.properteis") ;// 指定要操作的文件try{pro.load(new FileInputStream(file)) ;// 读取属性文件}catch(FileNotFoundException e){e.printStackTrace() ;}catch(IOException e){e.printStackTrace() ;}System.out.println("1、BJ属性存在:" + pro.getProperty("BJ")) ;System.out.println("2、SH属性存在:" + pro.getProperty("SH")) ;}};
将属性保存在XML文件中:
public class PropertiesDemo04{public static void main(String args[]){Properties pro = new Properties() ;// 创建Properties对象pro.setProperty("BJ","BeiJing") ;// 设置属性pro.setProperty("TJ","TianJin") ;pro.setProperty("NJ","NanJing") ;File file = new File("D:" + File.separator + "area.xml") ;// 指定要操作的文件try{pro.storeToXML(new FileOutputStream(file),"Area Info") ;// 保存属性到普通文件}catch(FileNotFoundException e){e.printStackTrace() ;}catch(IOException e){e.printStackTrace() ;}}};
从XML文件中读取属性:
public class PropertiesDemo05{public static void main(String args[]){Properties pro = new Properties() ;// 创建Properties对象File file = new File("D:" + File.separator + "area.xml") ;// 指定要操作的文件try{pro.loadFromXML(new FileInputStream(file)) ;// 读取属性文件}catch(FileNotFoundException e){e.printStackTrace() ;}catch(IOException e){e.printStackTrace() ;}System.out.println("1、BJ属性存在:" + pro.getProperty("BJ")) ;}};
3 0
- Java属性类:Properties的常用方法
- java读取Properties属性文件的方法
- java属性文件properties常用操作工具类
- java中遍历properties属性文件的方法
- 使用JAVA读写Properties属性文件的方法详解
- 使用JAVA读写Properties属性文件的方法详解
- Java中Properties类的常用操作
- java中properties文件的常用读取方法
- Java读取.properties资源文件的常用2种方法
- 常用的properties读取方法
- java读取properties属性文件值方法
- java读取properties属性文件值方法
- java读写Properties属性文件公用方法
- java读写Properties属性文件公用方法
- java读写Properties属性文件公用方法
- Java类集--属性类Properties
- java类集---属性类:Properties
- java.util.Properties类的load()方法
- dom4j 解析 xml文件
- java中的移位 <<<
- Caused by: java.lang.ClassNotFoundException: org.hibernate.engine.FilterDefinition
- django学习笔记(模板)
- [LeetCode]Copy List with Random Pointer
- Java属性类:Properties的常用方法
- java 垃圾回收之Minor GC和Major GC
- 经典算法5一个矩阵中最大的二维矩阵(元素和最大).
- GetDlgItem 返回NULL
- KEIL 5.0或者KEIL 511A 破解 以及 建立工程 以及旧工程的移植
- 嵌入式 U-Boot
- OC中NSLog函数输出格式详解
- FATFS学习之——多任务支持
- TI C66x DSP 系统events及其应用 - 5.6(INTMUX)