13.10其他集合类(血干JAVA系类)

13.10.1 Stack 类

在这里插入图片描述
在这里插入图片描述

【例13.48】完成入栈及出栈程序

package jiaqi;

import java.util.*;

public class temp {

	public static void main(String[] args) {
		Stack<String> stack = new Stack<String>();
		stack.push("A");
		stack.push("B");
		stack.push("C");
		System.out.println(stack.pop());
		System.out.println(stack.pop());
		System.out.println(stack.pop());
		System.out.println(stack.pop());
	}
}

在这里插入图片描述
从程序的运行结果来看,先进去的内容最后才取出,而且如果栈已经为空,则无法再弹出,会出现空栈异常。

13.10.2 属性类 Properties

1. Properties 类简介

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

2. Properties操作实例

(1)实例操作1——设置和取得属性

【例13.49]设置和取得属性

可以使用setProperty()和getProperty()方法设置和取得属性,操作的时候要以String为操作类型。

package jiaqi;

import java.util.*;

public class temp {

	public static void main(String[] args) {
		Properties pro = new Properties();
		pro.setProperty("BJ", "BeiJing");
		pro.setProperty("NJ","NanJing");
		pro.setProperty("XJ","XiJing");
		System.out.println("BJ:"+pro.getProperty("BJ"));
		System.out.println("SC:"+pro.getProperty("SC"));
		System.out.println("SC:"+pro.getProperty("SC","不存在SC"));
	}
}

在这里插入图片描述

(2)实例操作2——将属性保存在普通文件中

正常属性类操作完成后,可以将其内容保存在文件中,那么直接使用store()方法即可,同时指定OutputStream类型,指明输出的位置。属性文件的扩展名是任意的,但是最好按照标准, 将属性文件的扩展名统一设置成“'properties”。

【例13.50】保存属性到普通的属性文件之中
package jiaqi;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.*;


public class temp {

	public static void main(String[] args) throws Exception
	{
		Properties pro = new Properties();
		pro.setProperty("BJ", "BeiJing");
		pro.setProperty("NJ","NanJing");
		pro.setProperty("XJ","XiJing");
		File f =new File("d:"+File.separator+"area.properties");
		OutputStream out = new FileOutputStream(f); 
		try
		{
			pro.store(out,"此处是注释!");
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
}

在这里插入图片描述

(3)实例操作3——从普通文件之中读取属性内容

既然可以保存,也可以通过load()方法,从输入流中将所保存的所有属性内容读取出来。

【例13.51】从属性文件中读取内容
package jiaqi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;


public class temp {

	public static void main(String[] args) throws Exception
	{
		Properties pro = new Properties();
		File f =new File("d:"+File.separator+"area.properties");
		InputStream input = new FileInputStream(f);
		
		try
		{
			pro.load(input);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		
		System.out.println(pro.getProperty("BJ"));
	}
}

在这里插入图片描述

(4)实例操作4——将属性保存在XML文件中

在Properties类中也可以把全部内容以XML格式通过输出流输出,如果要把属性保存在XML文件中,则文件的扩展名最好为”XXX.xml“。

【例13.52]将属性保存在XML文件之中
package jiaqi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;


public class temp {

	public static void main(String[] args) throws Exception
	{
		Properties pro = new Properties();
		pro.setProperty("BJ","Beijing");
		File f =new File("d:"+File.separator+"area.xml");
		OutputStream out = new FileOutputStream(f);
		
		try
		{
			pro.storeToXML(out,"XML文件");
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
}

在这里插入图片描述

(5)实例操作5——从XML文件中读取属性

以XML文件格式输出全部属性后,必须要使loadFromXML()方法将内容读取进来。

【例13.53】读取文件内容
package jiaqi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;


public class temp {

	public static void main(String[] args) throws Exception
	{
		Properties pro = new Properties();
		File f =new File("d:"+File.separator+"area.xml");
		InputStream input = new FileInputStream(f);
		
		try
		{
			pro.loadFromXML(input);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		System.out.println("BJ:"+pro.getProperty("BJ"));
	}
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿斯卡码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值