在java.util包下面有一个类Properties,该类主要用于读取以项目的配置文件(以.properties结尾的文件和xml文件)。
一. Properties结构
类继承结构如下:
class Properties extends Hashtable<Object,Object>
从上面可以看出来Properties继承自Hashtable。
二. 具体案例
案例一: 读取.properties文件。
首先建立一个.properties文件,内容如下:
#网站信息
website = http://www.swiftlet.net
author = admin
date = 2015年
java类如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties; public class ReadProperties
{
public static void main(String[] args)
{
File file = new File("c:\\test.properties");
FileInputStream in = null;
try
{
in = new FileInputStream(file);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
Properties p = new Properties();
try
{
p.load(in);
}
catch (IOException e)
{
e.printStackTrace();
}
p.list(System.out);
}
}
输出结果如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWsAAAB8CAIAAAAggAa4AAAMY0lEQVR4nO2dQbKDOA6Gc63kQFwnOcKcgppF76emqve9yKarpuYCc4HMIiRYtiQbAUmA76ssXmKwZVv+sQHrnf7z91///Nd///3X//b36f/48wEJ/R9/fr1T+Ozsc0JBjgMKwmfxDwpyIFAQPot/UJADgYLwWfwzKEj/x5+7/Hx7zP4WX+8OPvv7DArybd8GgE2CggBAHBQEAOKgIAAQBwUBgDi/oSD36/l0vt7XLKLv1i7h1w1I+UCD/wR9d+r6x7z6HqWtgqAgFvfr+TQy37rRAJFz18/MN8S8Bu87afpa4ni/nmc20NcUZGgi87whvfCAsil/wFtcYgpyv56/XpmJNoQUJB0jcyssFORlykevb4v12mtcJt/XqMT9ej6f55mcWzql7NltZTVL3wkt6Lv0qMQ5ih9+cjaEgrTlH3ZFxQDhJB9c3GxMQZ7NNK/hf1BBahejXEK+5S2tNCvIOKNMJmBDbcZfUrXs+r4bfuq70/naD9Oxrh/PSOb1z1btu9P5epWpDzGVa7ZBHpYmNGl56kPZCVpZuYWOAapPZC2WnfvOMjnMLkvrhfM5NaOYYCktZE+cPQXJzxRNN37xC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j//YoRp+yKoXCgI7Zlas9riCqGeVsTfSYEFZIKHWQMlW0bax75xHe5oVRKsXCgI7pkVBzFCGKyiIHas0eKPCMN6J3oiCADRTVRAvlOHH5yCqgjirGMd45iAAC9CmIEYoQ+U/NTREOXR0R7kPIoqeOgfxjDejN6IgAM203UlNru0ylKF48iACICqnimcxjzSL7Kv6tCO2imkzXkRvdBVEmfDUZmQoCOwY9sWsDgoCOwYFWR0UBHYMCrI6KAjsGBRkdVAQ2DEoCADEQUEAIA4KAgBxUBAAiIOCAEAcFAQA4qAgABAHBQGAOCgIAMRBQQAgDgoCAHFQEACIg4IAQBwUBADioCAAEAcFAYA4KAgAxEFBACAOCgIAcVAQAIiDggBAHBQEAOL8Hz+qHhLlFP+6AAAAAElFTkSuQmCC" alt="" />
案例二: 读取.xml文件。
首先建立一个.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="website">swiftlet.net</entry>
<entry key="author">admin</entry>
</properties>
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties; public class ReadXml
{
public static void main(String[] args)
{ File file = new File("c:\\test.xml");
FileInputStream in = null;
try
{
in = new FileInputStream(file);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
Properties p = new Properties();
try
{
p.loadFromXML(in);
}
catch (IOException e)
{
e.printStackTrace();
}
p.list(System.out);
}
}
输出结果如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQMAAABRCAIAAABZglTUAAAIO0lEQVR4nO1dy5Wsug51WhCQ04EQXhQenQTuWmd+B2TxEvAdQIFlfWx+xaf2Xj3oxliSZW1bUFaX++ff/7/vJ/z5G4EE4c/fyyfl5j8u/Pn7yp+rY+9euHw67v/jrp4jALgFwAQAiBFMAIARYAIAxAgmAMCIq5kwdI1ruuFMFcGfreF8A4J3PhxkzhX42L9nuk8OFTCBY+gat2C/db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ADGCCQAwAkwAgBjBBAAYASYAQIxgAgCMABMAIEYwAQBGfP3/ot7v48p1VpnVm8BzcQ4T1p2tvhhgAhDBhHhXq4AvA0y4qVXAl1FiQnpMTP/OLlLCw5DVii330CRD+yLKT43AYkpt3CrG502zxKFvne9HM5ZiR+EIHj+obdVGAk+AyYTxoP4EUuhVKGyyS3OlGkJaRkZrF6Zu/CslzSIE3fjs1Ohi7MjFqSzBzfQrFg8Y4wKeghXZUV6GuY0JYi/zS8ekqrfVkVZVXip8MWY1E36xzOtVsJnAkp0TmaDXlmyMLMV4o2YdTPhhFKo3tWeDr+8Jalmkkh0ZxmNPAASUmDDPKC3gEv6hCitO48FgRIzwnEBUr40sy/gk2/rsG2DCz6P0xJystYFUt5M3JbzwXa1X1COGrPAVO0wBdcaTqn2TCUaxJZjwAuDcEQDECCYAwAgwAQBiBBMAYITKBOdAEuCH8B+YqKorrDEO8QAAAABJRU5ErkJggg==" alt="" />
三. 常见问题:
Invalid byte 1 of 1-byte UTF-8 sequence. 产生这个异常的原因是:
所读的xml文件实际是GBK或者其他编码的,而xml内容中却用<?xml version="1.0" encoding="utf-8"?>指定编码为utf-8,所以就报异常了!常见的解决访问有两种:
第一:可以直接在XML文件中更改UTF-8为GBK或GB2312
<?xml version="1.0" encoding="GB2312"?>
第二:将xml文件的编码格式修改为utf-8重新保存一下就可以了。