黑马程序员 java Properties FileInputStream 属性设置 文件读写

package itcast.video0101;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


import javax.swing.filechooser.FileSystemView;


public class I_01_Property {


/**
* @param args
*            properties是Hashtable的子类 也就是说它具备Map集合的特点,而且它里面存储的键值对都是字符串
*            是集合中和IO技术结合的集合容器 该对象特点:可以用于键值对形式配置文件
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String path = "C:\\Users\\Administrator\\Desktop\\book2\\";


// properties是HashTable的子类,也就是Map集合的一个子类对象
// 那么可以通过Map的方法取出该集合中的元素
// 该集合中存储都是字符串。没有范型定义。


// 在系统中自定义一些特有信息
System.setProperty("myKey", "myValue");
// 获取系统中指定属性信息
System.out.println(System.getProperty("myKey"));


// 设置 与 获取 Proterties属性
// setAndGet();
// System.out.println("proPerties: "+System.getProperty("lisi"));


// 用流读取文件 吧读取到的键值对保存到Properties
// readerProperties("C:\\Users\\Administrator\\Desktop\\book2\\properties.txt");


// 用Properties中load加载文件 吧读取到的键值对保存到Properties
// loadFileProperties("C:\\Users\\Administrator\\Desktop\\book2\\properties.txt");


// 模拟游戏中计数
runCount(path + "count.txt");


}


// 用Properties中load加载文件 吧读取到的键值对保存到Properties
public static void loadFileProperties(String file) throws IOException {
Properties prop = new Properties();
FileInputStream in = new FileInputStream(file);
prop.load(in);


// 用properties修改硬盘文件值
FileOutputStream out = new FileOutputStream(file);
prop.setProperty("lisi", "100");
prop.store(out, "使用Properties修改本地文件");


System.out.println(prop);
prop.list(System.out);


in.close();
out.close();


}


// 模拟游戏中计数
public static void runCount(String fileName) throws IOException {


File file = new File(fileName);
if (file.isDirectory()) {
file.mkdirs();
}


if (!file.isFile()) {
file.createNewFile();
}
int count = 0;
FileInputStream fis = new FileInputStream(file);
Properties prop = new Properties();
prop.load(fis);


String countStr = prop.getProperty("count");
if (countStr != null){
count = Integer.parseInt(countStr);}
if(count>=5){
System.out.println("使用次数已到,请付费!!");
}
count++;
prop.setProperty("count", count + "");

FileOutputStream fos = new FileOutputStream(file);
prop.store(fos, "模拟游戏计数程序");


fos.close();
fis.close();


System.out.println("end runCount");


}


// 用流读取文件 吧读取到的键值对保存到Properties
public static void readerProperties(String file) throws IOException {
BufferedReader bufr = new BufferedReader(new FileReader(file));
Properties prop = new Properties();
String line = null;
while ((line = bufr.readLine()) != null) {
String[] kv = new String[2];
kv = line.split("=");
prop.setProperty(kv[0], kv[1]);
}
System.out.println(prop);
}


/*
* 设置 获取 Properties 属性
*/
public static void setAndGet() {
Properties prop = new Properties();
prop.setProperty("zhangsan", "30");
prop.setProperty("lisi", "20");


String value = prop.getProperty("lisi");
System.out.println(value);
for (String p : prop.stringPropertyNames()) {
System.out.println(p + " : " + prop.getProperty(p));
}
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值