13.10其他集合类
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"));
}
}