Properties与IO流结合案例

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/*分析以下需求,并用代码实现
实现一个验证程序运行次数的小程序,要求如下:
1.当程序运行超过3次时给出提示:本软件只能免费使用3次,欢迎您注册会员后继续使用~
2.程序运行演示如下:
	第一次运行控制台输出: 欢迎使用本软件,第1次使用免费~
	第二次运行控制台输出: 欢迎使用本软件,第2次使用免费~
	第三次运行控制台输出: 欢迎使用本软件,第3次使用免费~
	第四次及之后运行控制台输出:本软件只能免费使用3次,欢迎您注册会员后继续使用~*/

public class t1_2 {
	public static void main(String[] args) throws IOException {
		Properties prop = new Properties();
		
		//限制次数
		File file = new File("计数2.txt");
		if( !file.exists()) {
			file.createNewFile();
		}
		
		//创建字节输入流对象
		FileInputStream fis = new FileInputStream(file);
		prop.load(fis);
		fis.close();
		
		String result = prop.getProperty("count", "1");
		int count = Integer.parseInt(result);
		System.out.println(count);
		if(count >3) {
			System.out.println("本软件只能免费使用3次,欢迎您注册会员后继续使用~");
		}else {
			System.out.println("欢迎使用本软件,第"+count+"次使用免费~");
		}
		
		//将count加1再写回到文件中
		prop.setProperty("count", count+1+"");
		FileOutputStream fos = new FileOutputStream("计数2.txt");
		prop.store(fos, null);
		fos.close();
	}
}

猜你喜欢

转载自blog.csdn.net/ludadan/article/details/79947160
今日推荐