博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java I/O Properties的使用 存取配置文件
阅读量:7107 次
发布时间:2019-06-28

本文共 1400 字,大约阅读时间需要 4 分钟。

package com.io;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Properties;public class PropertyDemo {	/**	 * 通过配置文件记录访问次数,例子:程序的试用次数。	 */	public static void main(String[] args) {		FileInputStream fis = null;		FileOutputStream fos = null;		try {			//创建Properties对象			Properties prop = new Properties();			//将文件封装在File对象中			File file = new File("D:/conf.ini");			//判断文件是否存在,如果不存在就创建			if(!file.exists()){				file.createNewFile();			}			//将文件对象放入流对象中			fis = new FileInputStream(file);			//加载流文件			prop.load(fis);			//创建计数器			int count = 0;			//从配置文件中获取times的值(次数)			String times = prop.getProperty("times");			//判断times是否为空,如果有值存入count中			if(times!=null){				count = Integer.parseInt(times);			}			//每访问一次count变加1			count ++;			//将新的count写入prop中			prop.setProperty("times", count+"");			//创建输出流			fos = new FileOutputStream(file);			//将配置信息重新写入文件中,并加上注释信息comments(也可不写)			prop.store(fos, "hahha");		} catch (FileNotFoundException e) {			e.printStackTrace();		} catch (IOException e) {			e.printStackTrace();		} finally {			if(fis!=null)				try {					fis.close();				} catch (IOException e) {					e.printStackTrace();				}			if(fos!=null){				try {					fos.close();				} catch (IOException e) {					e.printStackTrace();				}			}		}	}}

  

转载于:https://www.cnblogs.com/limpoo/p/3313447.html

你可能感兴趣的文章
EZOJ #201
查看>>
蓝桥杯:算法提高 9-2 文本加密
查看>>
从零开始学android -- CilpDrawable 徐徐展开的风景
查看>>
js数组去重的方法
查看>>
LeetCode-151-Reverse Words in s String
查看>>
贴吧回复
查看>>
linux 获取本机外网IP
查看>>
CentOS 设置mysql的远程访问
查看>>
android学习笔记(一)
查看>>
web application 访问控制
查看>>
JWT能够干什么,不应该干什么?
查看>>
Python 读写文件 小应用:生成随机的测验试卷文件
查看>>
SwaggerUI--SosoApi
查看>>
Java实现视频网站的视频上传、视频转码、视频关键帧抽图, 及视频播放功能
查看>>
ActiveMQ消息队列介绍(转)
查看>>
web前端知识体系小结(转)
查看>>
从windows server的文件服务到分布式文件服务(五)
查看>>
由IDC机房测试谈主动工作教学实战案例!
查看>>
爱因斯坦计划最新进展(201710)
查看>>
MariaDB10 多源复制搭建演示
查看>>