package cn.stat.p1.file; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Properties; import java.util.Set; public class proc { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub //创建文件对像 File file=new File("info.txt"); //判断文件是否存在 if(!file.exists()) { file.createNewFile(); } //创建文件读取对像 FileReader fr=new FileReader(file); //创建属性集合 Properties pt=new Properties(); //读取属性集合 pt.load(fr); //输出到控制台 pt.list(System.out); //更改 pt.setProperty("lisi", "99"); //创建文件写入对像 FileWriter fw=new FileWriter(file); //将此 Properties 表中的属性列表写入对像 pt.store(fw,""); pt.list(System.out); fr.close(); } public static void cl2() throws FileNotFoundException, IOException { Properties pt=new Properties(); FileInputStream fis=new FileInputStream("info.txt"); pt.load(fis); pt.list(System.out); } public static void cl() throws FileNotFoundException, IOException { Properties pt=new Properties(); pt.setProperty("zhangsan","22"); pt.setProperty("lisi","332"); pt.setProperty("xiaoli","452"); pt.setProperty("nini","112"); FileOutputStream fos=new FileOutputStream("info.txt"); pt.store(fos,"names"); fos.close(); pt.list(System.out); Set<String> st=pt.stringPropertyNames(); for(String ss : st) { String value=pt.getProperty(ss); System.out.println(ss+" "+value); } } }
时间: 2024-10-12 15:58:04