毕向东_Java基础视频教程第20天_IO流(11~14)
第20天-11-IO流(Properties简述)
.properties是一种主要在Java相关技术中用来存储应用程序的可配置参数的文件的文件扩展名。它们也可以存储用于国际化和本地化的字符串,这种文件被称为属性资源包(Property Resource Bundles)。
第20天-12-IO流(Properties存取)
package bxd; import java.io.IOException;
import java.util.Properties;
import java.util.Set; /*
Properties是hashtable的子类, 也就是说它具备map集合的特点, 而且它里面存储的键值对都是字符串, 是集合中和IO技术相结合的集合容器.
该对象的特点: 可以用于键值对形式的配置文件
*/
public class PropertiesDemo_1 { // 以下方法没有关联文件, 都是在内存对象中进行操作
public static void setAndGet() {
Properties properties = new Properties(); // 设置Properties元素
properties.setProperty("zhangsan", "30");
properties.setProperty("lisi", "40");
System.out.println(properties); // 获取Properties值
System.out.println("getProperty(\"zhangsan\"): " + properties.getProperty("zhangsan"));
System.out.println("getProperty(\"lisi\"): " + properties.getProperty("lisi")); // 修改Properties值
properties.setProperty("lisi", "89");
Set<String> names = properties.stringPropertyNames();
for (String s : names) {
System.out.println(s + ":" + properties.getProperty(s));
}
} public static void main(String[] args) throws IOException {
setAndGet();
}
}
第20天-13-IO流(Properties存取配置文件)
package bxd; import java.io.*;
import java.util.Properties; public class PropertiesDemo_2 { // 演示: 如何将流中的数据存储到集合中, 想要将message.properties中键值数据存到集合中继续操作
// 1. 用一个输入流和文件进行关联
// 2. 读取一行数据, 将该行数据以"="作为分隔符进行切分, 结果存入字符串数组
// 3. 等号左边为键, 右边为值, 存入到Properties对象中
public static void method_1() throws IOException {
Properties properties = new Properties();
BufferedReader bufr = new BufferedReader(new FileReader("messages.properties")); String line;
while ((line = bufr.readLine()) != null) {
String[] arr = line.split("=");
properties.setProperty(arr[0], arr[1]);
}
System.out.println(properties);
} public static void loadDemo() throws IOException {
Properties properties = new Properties();
FileInputStream fis = new FileInputStream("messages.properties"); // 加载
properties.load(fis);
System.out.println(properties); // 打印, This method is useful for debugging.
properties.list(System.out);
} public static void storeDemo() throws IOException {
Properties properties = new Properties();
FileOutputStream fileOutputStream = new FileOutputStream("info.txt");
properties.setProperty("key_one", "value_one"); // 保存
properties.store(fileOutputStream, "just for test");
} public static void main(String[] args) throws IOException {
storeDemo();
}
}
第20天-14-IO流(Properties练习)
package bxd; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties; /*
键值对数据是map集合, 数据是以文件形式存储, 使用IO技术, 即map + IO --> Properties; 如果处理xml, 则常常使用dom4j.
*/
public class PropertiesDemo_3 { public static void main(String[] args) throws IOException {
Properties properties = new Properties();
File file = new File("info.txt"); if (!file.exists()) file.createNewFile();
properties.load(new FileInputStream(file)); int count = 0;
String value = properties.getProperty("time");
if (value != null) {
count = Integer.parseInt(value);
if (count >= 5) {
System.out.println("对不起, 使用次数已到!");
return;
}
}
count++; properties.setProperty("time", count + "");
properties.store(new FileOutputStream(file), " ");
}
}
毕向东_Java基础视频教程第20天_IO流(11~14)的更多相关文章
- 毕向东_Java基础视频教程第20天_IO流(7~10)
第20天-07-IO流(递归) package bxd; import java.io.File; public class FileDemo3 { // 非递归打印 public static vo ...
- 毕向东_Java基础视频教程第20天_IO流(15~17)
第20天-15-IO流(打印输出流) 打印输出流:PrintWriter与PrintStream 两者的区别:Since JDK 1.4 it's possible to specify the ch ...
- 毕向东_Java基础视频教程第20天_IO流(5~6)
第20天-05-IO流(文件列表一) static File[] listRoots() List the available filesystem roots. String[] list() Re ...
- 毕向东_Java基础视频教程第20天_IO流(1~4)
第20天-01-IO流(File概述) File类: 用来将文件或者文件夹封装成对象, 方便进行操作. File对象可以作为参数, 传递给流对象的构造函数. 流对象不能操作文件夹; 流对象不能操作文件 ...
- 毕向东_Java基础视频教程第19天_IO流(20~22)
第19天-20-IO流(改变标准输入输出设备) static void setIn(InputStream in) Reassigns the "standard" input s ...
- 毕向东_Java基础视频教程第19天_IO流(01~05)
第19天-01-IO流(BufferedWriter) 字符流的缓冲区 缓冲区的出现提高了对数据的读写效率. 对应类缓冲区要结合流才可以使用. BufferedWriter BufferedReade ...
- 毕向东_Java基础视频教程第19天_IO流(06~10)
第19天-06-IO流(装饰设计模式) 装饰设计模式: 当想要对已有的对象进行功能增强时, 可以定义类,将已有对象传入,基于已有的功能,并提供加强功能.那么这个自定义的类称为装饰类. 装饰类通常会通过 ...
- 毕向东_Java基础视频教程第19天_IO流(11~14)
第19天-11-IO流(字节流File读写操作) import java.io.FileInputStream; import java.io.FileOutputStream; import jav ...
- 毕向东_Java基础视频教程第21天_IO流(1)
第21天-01-IO流(对象的序列化) ObjectInputStream与ObjectOutputStream 被操作的对象需要实现Serializable接口(标记接口) 非必须, 但强烈建议所有 ...
随机推荐
- suse-Linux下安装Oracle11g服务器
系统要求 Linux安装Oracle系统要求 系统要求 说明 内存 必须高于1G的物理内存 交换空间 一般为内存的2倍,例如:1G的内存可以设置swap 分区为3G大小 硬盘 5G以上 2.修改操作系 ...
- (转)MySQL高可用架构之MHA
MySQL高可用架构之MHA 原文:http://www.cnblogs.com/gomysql/p/3675429.html 简介: MHA(Master High Availability)目前 ...
- python-poll实现异步IO
#!usr/bin/python from socket import * from select import * from time import ctime s=socket() s.bind( ...
- 大数乘法的C代码实现
在C语言中,宽度最大的无符号整数类型是unsigned long long, 占8个字节.那么,如果整数超过8个字节,如何进行大数乘法呢? 例如: $ python Python 2.7.6 (def ...
- vlog常用参数解析
1. -f <filelist> : compile all files in filelist --------------------------------------------- ...
- [Mysql 查询语句]——对查询结果进一步的操作
distinct 不显示重复的查询结果 (1) 对于表中一些没有唯一性约束的字段,可能存在重复的值,这时可以使用distinct来消除那些查询结果中的重复值 select cust_id from ...
- html锚点使用示例
<html> <body> <h1>HTML 教程目录</h1> <ul> <li><a href="#C1&q ...
- mac下如何找到hosts文件(转)
打开Finder 在菜单中选择[前往][前往文件夹] 或使用快捷键Command+Shift+G 进入跳转路径,输入: /private/etc/ 点击[前往] 即可找到hos ...
- ASP.NET页面支持的指令
页面的处理指令 页面指令的处理用于配置执行该页面的运行时环境.在ASP.NET中,指令可以位于页面的任何位置,但良好且常见的习惯是将其置于文件的开始部分.除此,页面指令的名称是不区分大小写的,且指令的 ...
- VMwear安装Centos7详细过程
1.软硬件准备软件:推荐使用VMwear,我用的是VMwear 10镜像:CentOS7 硬件:因为是在宿主机上运行虚拟化软件安装centos,I3CPU双核.硬盘500G.内存4G以上. 2 ...