/**
- Created by cxh on 17/07/21.
*/
public class Main {
public static void main(String[] args) throws Exception {
Properties prop=new Properties();
FileInputStream fis=new FileInputStream("/Users/cxh/IdeaProjects/JavaBaseTest/src/bin/sample.properties");
prop.load(fis);
prop.list(System.out);
System.out.println();
System.out.println("The property is : "+prop.getProperty("id"));
}
}
测试结果:
The property is : cxh
Process finished with exit code 0
/**
- Created by cxh on 17/07/21.
*/
public class Main {
public static void main(String[] args) {
Properties p=new Properties();
p.setProperty("username","cxh1005");
p.setProperty("sex","女");
p.setProperty("schoolNum","201609080");
try{
PrintStream fw=new PrintStream("/Users/cxh/IdeaProjects/JavaBaseTest/src/bin/printStream.properties");
p.list(fw);//将内存中内容写入到fw的目录文件中
}catch (IOException e){
e.printStackTrace();
}
}
}
结果:
那,我们会发现,生产文件中的内容顺序和我们设定的不一样。因为Properties类实现类map接口,它是用key-value存储数据的,数据也是没有顺序保证的。
/**
- Created by cxh on 17/07/21.
*/
public class Main {
public static void main(String[] args) {
Properties p=new Properties();
p.setProperty("IP","127.0.0.1");
p.setProperty("Mac","98:73:41:ac:0f:c2");
try{
PrintStream ps=new PrintStream(new File("/Users/cxh/IdeaProjects/JavaBaseTest/src/bin/printStreams.xml"));
p.storeToXML(ps,"testPrintToXMLFile");
}catch(IOException e){
e.printStackTrace();
}
}
}
测试结果:
testPrintToXMLFile
3.2、读取.xml文件里面的内容
testPrintReadFromXMLFile
3.2.2、读取<span style="font-size:14px;">readSteam.xml文件里面的内容:
/**
- Created by cxh on 17/07/21.
*/
public class Main {
public static void main(String[] args) throws Exception{
Properties p=new Properties();
FileInputStream fis=new FileInputStream("/Users/cxh/IdeaProjects/JavaBaseTest/src/bin/readStream.xml");
p.loadFromXML(fis);
p.list(System.out);
System.out.println(); //空行
System.out.println("the Mac property is :"+p.getProperty("Mac"));
}
}
输出内容:
the Mac property is :98:73:41:ac:0f:c2
Process finished with exit code 0
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|