内容:Properties基本存取、存储到持久化设备、从持久化设备读取、简单模拟收费软件试用结束
##
Properties——有配置文件的功能。
    特点:
    1、Hash table的子类,map集合中的方法都可以用
    2、该集合没有泛型。键值都是字符串
    3、是一个持久化的属性集,键值可以保存到集合和持久化设备上,键值的来源也可以是持久化设备。

##########################################################
基本存取

public class PropertiesDemo
{
public static void main(String[] args)
{
methodDemo();
}
public static void methodDemo()
{
//Properties基本存取
Properties prop = new Properties();
prop.setProperty("xlsg", "21");
prop.setProperty("qldk", "20");
// prop.list(System.out); //这个方法调试使用
Set<String> set = prop.stringPropertyNames();
for(String name:set){
String value = prop.getProperty(name);
System.out.println(name+"__"+value);
}
}
}

#######################################################################
存储到持久化设备

通过Properties的store方法指定保存到什么文件里面

public class PropertiesDemo
{
public static void main(String[] args) throws IOException
{
methodDemo();
} public static void methodDemo() throws IOException
{
//Properties基本存取
Properties prop = new Properties();
prop.setProperty("xlsg", "21");
prop.setProperty("qldk", "20"); // prop.list(System.out); //这个方法调试使用 Set<String> set = prop.stringPropertyNames();
for(String name:set){
String value = prop.getProperty(name);
System.out.println(name+"__"+value);
} //永久化存储到设备上
FileOutputStream fos = new FileOutputStream("myfile\\we.properties"); //使用properties的store方法。
prop.store(fos, "my demo , person info");
} }

#########################################################################################
从持久化设备读取

public static void main(String[] args) throws IOException
{
// methodDemo();
methodDemo_read();
} private static void methodDemo_read() throws IOException
{
File configure_file = new File("myfile\\we.properties");
//读取流中的数据
Properties prop = new Properties(); //定义读取流和数据文件关联
FileInputStream fis = new FileInputStream(configure_file.toString());
prop.load(fis); prop.list(System.out); //测试使用 prop.setProperty("xlsg", "22"); prop.list(System.out); FileOutputStream fos = new FileOutputStream(configure_file); prop.store(fos, "my demo,person info"); fis.close();
fos.close();
}

步骤:1)创建properties对象
    2)创建输入流对象
    3)properties对象和输入流对象相关联
    4)通过prop.setProperty("xlsg", "22");进行操作
    5)保存prop.store(fos, "my demo , person info");

#########################################################################################
简单模拟收费软件试用结束

public static void main(String[] args) throws IOException
{
String value = read_count();
if(count(Integer.parseInt(value))){
//操作
value = Integer.toString(Integer.parseInt(value)+1);
System.out.println(value);
}
else{
System.out.println("试用结束,请充值!");
}
write_count(value);
} private static boolean count(int num)
{
if(num<5){
return true;
}
return false;
} private static void write_count(String value) throws IOException
{
File configure_file = new File("myfile\\count.properties");
FileOutputStream fos = new FileOutputStream(configure_file);
Properties prop = new Properties();
prop.setProperty("count", value);
prop.store(fos, "tryout count");
fos.close();
} private static String read_count() throws IOException
{
File configure_file = new File("myfile\\count.properties");
if(!configure_file.exists()){
configure_file.createNewFile();
}
FileInputStream fis = new FileInputStream(configure_file); Properties prop = new Properties();
prop.load(fis);
String value;
String key = "count";
if(prop.containsKey(key)){
}
else{
prop.setProperty(key, "0");
}
value = prop.getProperty(key);
return value; }

my code

public static void main(String[] args) throws IOException {
/*
* 练习:定义功能记录程序运行次数,满足试用次数后,给出提示:试用次数已到,请注册。
*
* 思路:
* 1,需要计数器。这个软件使用一次计数一次。每使用一次,就进行计数累计。
* 2,计数器是程序中的一个变量,程序启动计数器计数,可是程序结束这个计数器就消失了。
* 下次启动会重新进行计数,原来计数的值没有保留下来。咋办?
* 3,让这个计数器持久化。存储到文件中,为了标识数据可读性,数据起个名字。出现键值对。
* 而且还是一个持久化的键值对,Properties集合正好符合这个要求。
*
*/
if(isStop()){
System.out.println("试用次数已到,请注册");
return;
}
runcode();
} private static boolean isStop() throws IOException {
//1,配置文件。
File configFile = new File("tempfile\\app.properties");
if(!configFile.exists()){//如果配置文件不存在,就创建。
configFile.createNewFile();
}
//2,创建属性集。
Properties prop = new Properties();
//3,定义读取流和配置文件关联。
FileInputStream fis = new FileInputStream(configFile);
//4,将流关联的数据读取到属性集中。
prop.load(fis);
//5,通过属性集的指定键count,获取具体的次数。
String value = prop.getProperty("count");
int count = 0;
//6, 对value进行判断,如果存在就对其自增。
if(value!=null){
count = Integer.parseInt(value);
if(count>=5){
return true;
}
}
count++;//对其值进行自增。
//7,将自增后的值和指定的键重新存储到属性集中,键相同,值覆盖。
prop.setProperty("count", Integer.toString(count));
//8,将属性集存储到配置文件中。对配置文件中的信息进行更新。
FileOutputStream fos = new FileOutputStream(configFile);
prop.store(fos, "app run count");
//9,关闭资源。
fos.close();
fis.close();
return false;
}
//程序主体。
public static void runcode(){
System.out.println("程序运行....play");
}

the answer

021.8 properties(开发使用频率高)的更多相关文章

  1. 微信小程序开发日记——高仿知乎日报(中)

    本人对知乎日报是情有独钟,看我的博客和github就知道了,写了几个不同技术类型的知乎日报APP要做微信小程序首先要对html,css,js有一定的基础,还有对微信小程序的API也要非常熟悉 我将该教 ...

  2. 微信小程序开发日记——高仿知乎日报(上)

    本人对知乎日报是情有独钟,看我的博客和github就知道了,写了几个不同技术类型的知乎日报APP 要做微信小程序首先要对html,css,js有一定的基础,还有对微信小程序的API也要非常熟悉 我将该 ...

  3. 配置开发支持高并发TCP连接的Linux应用程序全攻略

    http://blog.chinaunix.net/uid-20733992-id-3447120.html http://blog.chinaunix.net/space.php?uid=16480 ...

  4. PHP 比 Java 的开发效率高在哪?

    PHP 比 Java 的开发效率高在哪? 现在很多互联网应用都是php开发的,在很多人的观念里已经把php与java分到了两个开发领域,php是互联网,java是企业应用. 都说php的开发效率高,更 ...

  5. 为什么golang的开发效率高(编译型的强类型语言、工程角度高、在开发上的高效率主要来自于后发优势,编译快、避免趁编译时间找产品妹妹搭讪,既是强类型语言又有gc,只要通过编译,非业务毛病就很少了)

    作者:阿猫链接:https://www.zhihu.com/question/21098952/answer/21813840来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...

  6. 笨重的mfc还在基于系统控件,熟练的mfc工程师还比不过学习Qt一个月的学生开发效率高(比较精彩,韦易笑)

    作者:韦易笑链接:https://www.zhihu.com/question/29636221/answer/45102191来源:知乎著作权归作者所有,转载请联系作者获得授权. 更新:擦,本来只有 ...

  7. 面系那个对象开发原则.高内聚.低耦合+Python安装详细教程+print输出带颜色的方法

    面系那个对象开发原则.高内聚.低耦合 软件设计中通常用耦合度和内聚度作为衡量模块独立程度的标准.划分摸块的一个准则就是高内聚低耦合. 这是软件工程中的概念,是判断设计好坏的标准,主要是面向OO的设计, ...

  8. 微信小程序开发日记——高仿知乎日报(下)

    本人对知乎日报是情有独钟,看我的博客和github就知道了,写了几个不同技术类型的知乎日报APP 要做微信小程序首先要对html,css,js有一定的基础,还有对微信小程序的API也要非常熟悉 我将该 ...

  9. 使用Lua脚本语言开发出高扩展性的系统,AgileEAS.NET SOA中间件Lua脚本引擎介绍

    一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...

随机推荐

  1. ASP.NET Core 集成 WebSocket

    1. 环境 AspNetCore Web 2.0 (MVC) Windows 10 IIS 10 Express/IIS VS 2017 2.如何配置 在已有的或者新创建的 AspNet Core M ...

  2. C/C++编码规范

    C/C++编码规范 今天人们越来越明白软件设计更多地是一种工程,而不是一种个人艺术.由于大型产品的开发通常由很多的人协同作战,如果不统一编程规范,最终合到一起的程序,其可读性将较差,这不仅给代码的理解 ...

  3. Solr后台管理

    Solr web管理后台 访问主页:http://localhost:8080/solr/#/ 1. Dashboard 仪表盘,显示Solr的基本信息,包含solr版本,包含系统内存和jvm内存的使 ...

  4. 设计模式之——外观or门面模式

    1.概念 定义一个高层的统一的外观接口类,该接口用于客户端调用,和一个实现类用来包装子系统中多个类,客户端可以通过客户端完成对子系统的方法调用. 2.适用场景 2.1 代码移植,降低了现有系统的复杂度 ...

  5. 阿里巴巴的数据池DRUID

      使用了阿里巴巴的数据池管理: 监控DB池连接和SQL的执行情况 https://github.com/alibaba/druid/wiki/常见问题 https://www.cnblogs.com ...

  6. 进度监视器--ProgressMonitorInputStream

    进度监视器--ProgressMonitorInputStream ProgressMonitorInputStream 可以创建一个进度监视器,以监视读取输入流的进度.如果需要一段时间,将会弹出 P ...

  7. <meta name="viewport"content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">的作用

    本人对该标签理解不深,这里是复制了穆乙的文章:如果有人进来看到这篇文章,请按此https://www.cnblogs.com/pigtail/archive/2013/03/15/2961631.ht ...

  8. PDO drivers no value in Windows 或 ndefined class constant 'MYSQL_ATTR_USE_BUFFERED_QUERY'

    把办公室的drupal7.54版本放到自己的笔记本(OS:Windows10 Pro,php:7.0.9,mysql 5.7.11,apache:2.4)上运行不了,查看了各项配置应该没问题啊.之前还 ...

  9. C++学习笔记(2)----类模板和友元

    当一个类包含一个友元声明时,类与友元各自是否是模板是相互无关的.如果一个类模板包含一个非模板友元,则友元被授权可以访问所有模板实例.如果友元自身是模板,类可以授权给所有友元模板实例,也可以只授权给特定 ...

  10. 使用WICleanup清理Windows Installer 冗余文件

    使用WICleanup清理Windows Installer 冗余文件 | 浏览:816 | 更新:2015-11-02 10:43 | 标签:Win7 Win10 1 2 3 4 5 6 7 分步阅 ...