《Properties XML文件写出》

packageapistudy;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.io.UnsupportedEncodingException;
importjava.util.Properties;
publicclasspropertiesTest
{
publicstaticvoidmain(String[]args)
{
Stringreadfile="d:"+File.separator+"readfile.properties";
Stringwritefile="d:"+File.separator+"writefile.properties";
Stringreadxmlfile="d:"+File.separator+"readxmlfile.xml";
Stringwritexmlfile="d:"+File.separator+"writexmlfile.xml";
Stringreadtxtfile="d:"+File.separator+"readtxtfile.txt";
Stringwritetxtfile="d:"+File.separator+"writetxtfile.txt";
readPropertiesFile(readfile);//读取properties文件
writePropertiesFile(writefile);//写properties文件
readPropertiesFileFromXML(readxmlfile);//读取XML文件
writePropertiesFiletoXML(writexmlfile);//写XML文件
readPropertiesFile(readtxtfile);//读取txt文件
writePropertiesFile(writetxtfile);//写txt文件
}
//读取资源文件,并处理中文乱码
publicstaticvoidreadPropertiesFile(Stringfilename)
{
Propertiesproperties=newProperties();
try
{
InputStreaminputStream=newFileInputStream(filename);
properties.load(inputStream);
inputStream.close();//关闭流
}
catch(IOExceptione)
{
e.printstacktrace();
}
Stringusername=properties.getProperty("username");
Stringpasssword=properties.getProperty("password");
Stringchinese=properties.getProperty("chinese");
try
{
chinese=newString(chinese.getBytes("ISO-8859-1"),"GBK");//处理中文乱码
}
catch(UnsupportedEncodingExceptione)
{
e.printstacktrace();
}
System.out.println(username);
System.out.println(passsword);
System.out.println(chinese);
}
//读取XML文件,并处理中文乱码
publicstaticvoidreadPropertiesFileFromXML(Stringfilename)
{
Propertiesproperties=newProperties();
try
{
InputStreaminputStream=newFileInputStream(filename);
properties.loadFromXML(inputStream);
inputStream.close();
}
catch(IOExceptione)
{
e.printstacktrace();
}
Stringusername=properties.getProperty("username");
Stringpasssword=properties.getProperty("password");
Stringchinese=properties.getProperty("chinese");//XML中的中文不用处理乱码,正常显示
System.out.println(username);
System.out.println(passsword);
System.out.println(chinese);
}
//写资源文件,含中文
publicstaticvoidwritePropertiesFile(Stringfilename)
{
Propertiesproperties=newProperties();
try
{
OutputStreamoutputStream=newFileOutputStream(filename);
properties.setProperty("username","myname");
properties.setProperty("password","mypassword");
properties.setProperty("chinese","中文");
properties.store(outputStream,"author:shixing_11@sina.com");
outputStream.close();
}
catch(IOExceptione)
{
e.printstacktrace();
}
}
//写资源文件到XML文件,含中文
publicstaticvoidwritePropertiesFiletoXML(Stringfilename)
{
Propertiesproperties=newProperties();
try
{
OutputStreamoutputStream=newFileOutputStream(filename);
properties.setProperty("username","中文");
properties.storetoXML(outputStream,"author:shixing_11@sina.com");
outputStream.close();
}
catch(IOExceptione)
{
e.printstacktrace();
}
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇