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());
}
}
从程序的运行结果来看,先进去的内容最后才取出,而且如果栈已经为空,则无法再弹出,会出现空栈异常。
可以使用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"));
}
}
正常属性类操作完成后,可以将其内容保存在文件中,那么直接使用store()
方法即可,同时指定OutputStream类型,指明输出的位置。属性文件的扩展名是任意的,但是最好按照标准, 将属性文件的扩展名统一设置成“'properties”。
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();
}
}
}
既然可以保存,也可以通过load()
方法,从输入流中将所保存的所有属性内容读取出来。
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"));
}
}
在Properties类中也可以把全部内容以XML格式通过输出流输出,如果要把属性保存在XML文件中,则文件的扩展名最好为”XXX.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();
}
}
}
以XML文件格式输出全部属性后,必须要使loadFromXML()
方法将内容读取进来。
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"));
}
}