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));
}
}
}
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));
}
}
}