package FileDemo; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Properties; public class FileDemo { public static void main(String[] args) throws IOException { method_2(); } //使用次数配置信息练习 public static void method_2() throws IOException { File confile=new File("a.txt"); if(!confile.exists()) { confile.createNewFile(); } FileInputStream fis=new FileInputStream(confile); Properties prop=new Properties(); prop.load(fis); String value=prop.getProperty("time"); int count=0; if(value!=null){ count=Integer.parseInt(value); if(count>=5){ /* System.out.println("使用次数已到,给钱"); return;*/ throw new RuntimeException("使用次数已到,给钱"); } } count++; prop.setProperty("time", count+""); FileOutputStream fos=new FileOutputStream(confile); prop.store(fos, ""); prop.list(System.out); fis.close(); fos.close(); } public static void method_1() throws IOException { File file=new File("a.txt"); if(!file.exists()) { file.createNewFile(); } FileReader fr=new FileReader("a.txt"); Properties prop=new Properties(); prop.load(fr); prop.setProperty("wangwu", "12"); FileWriter fw=new FileWriter(file); prop.store(fw, ""); prop.list(System.out); fr.close(); } }