本帖最后由 zippo 于 2014-8-2 22:45 编辑
- package Error;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.util.Properties;
- //需求:设计一个程序,当程序运气第三次时停止运行。
- public class Runtimes {
- public static void main(String[] args) throws Exception {
- File file = new File("F:\\times.txt");
- FileWriter fileWriter = new FileWriter(file);
- FileReader fileReader = new FileReader(file);
-
- if (!file.exists()) {
- file.createNewFile();
- }
-
-
- Properties properties = new Properties();
- properties.load(fileReader);
-
- String a= properties.getProperty("times");
- int count=0;
-
- System.out.println(a);
- if (a!=null) {
- count =Integer.parseInt(a);
- System.out.println(count);
- }
-
- if (count==3) {
- System.out.println("run over of use~");
- System.exit(0);
- }
- count++;
- System.out.println("you have has used "+count+" times");
- properties.setProperty("times", count+"");
- properties.store(fileWriter, "runtimes");
- fileWriter.close();
- fileReader.close();
- }
- }
复制代码
|
|