首页 > 编程语言 >Java拾贝第十六天——其他集合类Properties

Java拾贝第十六天——其他集合类Properties

时间:2023-11-04 20:22:50浏览次数:37  
标签:Java String pro Properties void File public 拾贝

Properties

Properties是Map的子实现类,其定义如下

public class Properties extends Hashtable<Object,Object>

Hashtable是Map接口的一个子类,与Vector一样都是旧的操作类,与HashMap没什么区别。

Properties是操作属性文件的一个属性操作类。

属性文件中属性都是以字符串表示的 键值对 保存的     key=value
如果想要操作属性文件的属性 可以使用IO流配合Properties类完成

Windows中 .ini 后缀大多都是属性文件

Properties类常用方法如下:

方法 类型 描述
public String getProperty(String key) 普通方法 根据Key获得Value(没有则返回null)
public String getProperty(String key, String defaultValue) 普通方法 根据Key获得Value(没有则返回defaultValue)
public synchronized Object setProperty(String key, String value) 同步方法 设置属性
public void store(OutputStream out, String comments) throws IOException 普通方法 输出属性内容,同时声明属性的注释
public synchronized void load(InputStream inStream) throws IOException 同步方法 读取输入流的内容
public void storeToXML(OutputStream os, String comment) throws IOException 普通方法 以XML文件格式输出属性,同时声明注释
public synchronized void loadFromXML(InputStream in) throws IOException, InvalidPropertiesFormatException 同步方法 从XML文件中读取内容

public synchronized Object setProperty(String key, String value)

public String getProperty(String key)

public String getProperty(String key, String defaultValue)

设置属性和获取属性,以及获取属性的重载

栗子:

    public static void main(String[] args) {
        Properties pro = new Properties();
        pro.setProperty("3","可乐");
        pro.setProperty("4","大瓶冰红茶");
        pro.setProperty("2.5","罐装雪碧");

        System.out.println(pro.getProperty("3"));
        System.out.println(pro.getProperty("5"));
        System.out.println(pro.getProperty("5","王老吉"));//没有key则返回设置的内容
    }

程序运行结果:

可乐
null
王老吉

public void store(OutputStream out, String comments) throws IOException

输出属性内容,同时声明注释 (后缀是任意的,但最好按照标准设置为properties)

栗子:

    public static void main(String[] args) {
        Properties pro = new Properties();
        File file = new File("D:" + File.separator + "IOtest" + File.separator + "T.properties");
        pro.setProperty("3","可乐");
        pro.setProperty("4","大瓶冰红茶");
        pro.setProperty("2.5","罐装雪碧");

        try {
            pro.store(new FileWriter(file),"这里是注释");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

下个方法展示读取内容。

public synchronized void load(InputStream inStream) throws IOException

读取输入流的内容

栗子:

    public static void main(String[] args) {
        Properties pro = new Properties();
        File file = new File("D:" + File.separator + "IOtest" + File.separator + "T.properties");
        try {
            pro.load(new FileReader(file));//读取的内容在对象内
            System.out.println(pro);//Properties重写了toString方法
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

程序运行结果:

{4=大瓶冰红茶, 3=可乐, 2.5=罐装雪碧}

public void storeToXML(OutputStream os, String comment) throws IOException

输出成XML文件

栗子:

    public static void main(String[] args) {
        Properties pro = new Properties();
        File file = new File("D:" + File.separator + "IOtest" + File.separator + "op.XML");
        pro.setProperty("3","可乐");
        pro.setProperty("4","大瓶冰红茶");
        pro.setProperty("2.5","罐装雪碧");
        try {
           pro.storeToXML(new FileOutputStream(file),"我是注释");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

D:\IOtest\op.XML内:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>我是注释</comment>
<entry key="4">大瓶冰红茶</entry>
<entry key="2.5">罐装雪碧</entry>
<entry key="3">可乐</entry>
</properties>

public synchronized void loadFromXML(InputStream in) throws IOException, InvalidPropertiesFormatException

读取XML文件内容

栗子:

    public static void main(String[] args) {
        Properties pro = new Properties();
        File file = new File("D:" + File.separator + "IOtest" + File.separator + "op.XML");
        try {
            pro.loadFromXML(new FileInputStream(file));
            System.out.println(pro);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

程序运行结果:

{4=大瓶冰红茶, 3=可乐, 2.5=罐装雪碧}

标签:Java,String,pro,Properties,void,File,public,拾贝
From: https://www.cnblogs.com/Ocraft/p/17809739.html

相关文章

  • Java JDBC
    JDBC基本用法常用接口和类简介DriverManager类用于管理JDBC驱动的服务类。程序中使用该类的主要功能是获取Connection对象publicstaticsynchronizedConnectiongetConnection(Stringurl,Stringuser,Stringpass)throwsSQLException获取url对应的数据库连接C......
  • java 异常捕获
    HTTP响应主体的方式不同: a.@RestController:   1.返回的对象数据作为JSON/XML直接写入HTTP响应.   2.是@Controller+@ResponseBody组合注解.获取请求参数方式:在方法写形参获取=>适合少量的定义一个对象存储提交的字段=>适合量多的3.在方法写形参:3.1Ge......
  • JavaSE(09) - 面向对象进阶
    JavaSE(09)-面向对象进阶p121static关键字static表示静态,是java中的一个修饰符,可以修饰成员方法,成员变量.一,被static修饰的成员变量,叫做静态变量.特点:被改类所有对象共享不属于对象属于类随着类的加载而加载,优先于对象存在调用方式:类名调用(推荐)对......
  • JavaSE(10) - 面向对象进阶
    JavaSE(10)-面向对象进阶P129认识多态(polymorphism)多态就是对象的多种形态多态的前提是:1,有继承/实现关系2,有父类引用指向子类对象3,有方法重写多态的好处:使用父类型作为参数,可以接收所有子类对象,体现多态的扩展性与便利P130多态调用成员的特点调用......
  • 学JAVA用PYTHON重写day02.5
    packageday02;publicclassDemo05{/*三个数字排序*/publicstaticvoidmain(String[]args){System.out.println("三个数字排序,从大到小:");inta=2;intb=5;intc=1;intt;if(a<b){......
  • Java拾贝第十六天——集合之Queue、Stack
    Queue(队列)Queue是一种先进先出(FIFO:FirstInFirstOut)的有序集合:Queue是Collection的子接口,其定义如下publicinterfaceQueue<E>extendsCollection<E>LinkedList实现了Queue的子接口,根据多态性可以使用Queue创建LinkedList实例。Queue接口常用方法如下:方法类型......
  • JavaScript知识点
    成员对象1、window.eventwindow.documentwindow.history2、window.screenwindow.navigatorwindow.externalWindow对象的属性如下1、window//窗户自身2、window.self//引用本窗户window=window.self3、window.name//为窗户命名4、window.defaultStatus//设定窗户状态栏信息5、w......
  • JavaScript知识点
    同源限制1、同源策略指的是∶协议,域名,端口相同,同源策略是一种安全协议2、举例说明:比如一个黑客程序,他利用lframe把真正的银行登录页面嵌到他的页面上,当你使用真实的用户名,密码登录时,他的页面就可以通过Javascript读取到你的表单中input中的内容,这样用户名,密码就轻松到手了。offse......
  • 学JAVA用PYTHON重写day02.4
    packageday02;publicclassDemo04{/*判断是否是闰年普通年,能被4整除且不能被100整除的为闰年。(y%4==0)&&(y%100!=0)世纪年,能被400整除的是润年。y%400==0。四年一闰,百年不闰,四百又闰*/publicstaticvoidmain(......
  • JavaScript知识点
    null,undefined的区别1、undefined表示不存在这个值。2、undefined:是一个表示"无"的原始值或者说表示"缺少值",就是此处应该有一个值,但是还没有定义。当尝试读取时会返回undefined3、例如变量被声明了,但没有赋值时,就等于undefined4、null表示—个对象被定义了,值为“空值”5、null......