第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)的更多相关文章

  1. 毕向东_Java基础视频教程第20天_IO流(7~10)

    第20天-07-IO流(递归) package bxd; import java.io.File; public class FileDemo3 { // 非递归打印 public static vo ...

  2. 毕向东_Java基础视频教程第20天_IO流(15~17)

    第20天-15-IO流(打印输出流) 打印输出流:PrintWriter与PrintStream 两者的区别:Since JDK 1.4 it's possible to specify the ch ...

  3. 毕向东_Java基础视频教程第20天_IO流(5~6)

    第20天-05-IO流(文件列表一) static File[] listRoots() List the available filesystem roots. String[] list() Re ...

  4. 毕向东_Java基础视频教程第20天_IO流(1~4)

    第20天-01-IO流(File概述) File类: 用来将文件或者文件夹封装成对象, 方便进行操作. File对象可以作为参数, 传递给流对象的构造函数. 流对象不能操作文件夹; 流对象不能操作文件 ...

  5. 毕向东_Java基础视频教程第19天_IO流(20~22)

    第19天-20-IO流(改变标准输入输出设备) static void setIn(InputStream in) Reassigns the "standard" input s ...

  6. 毕向东_Java基础视频教程第19天_IO流(01~05)

    第19天-01-IO流(BufferedWriter) 字符流的缓冲区 缓冲区的出现提高了对数据的读写效率. 对应类缓冲区要结合流才可以使用. BufferedWriter BufferedReade ...

  7. 毕向东_Java基础视频教程第19天_IO流(06~10)

    第19天-06-IO流(装饰设计模式) 装饰设计模式: 当想要对已有的对象进行功能增强时, 可以定义类,将已有对象传入,基于已有的功能,并提供加强功能.那么这个自定义的类称为装饰类. 装饰类通常会通过 ...

  8. 毕向东_Java基础视频教程第19天_IO流(11~14)

    第19天-11-IO流(字节流File读写操作) import java.io.FileInputStream; import java.io.FileOutputStream; import jav ...

  9. 毕向东_Java基础视频教程第21天_IO流(1)

    第21天-01-IO流(对象的序列化) ObjectInputStream与ObjectOutputStream 被操作的对象需要实现Serializable接口(标记接口) 非必须, 但强烈建议所有 ...

随机推荐

  1. 转 在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()

    在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException:  Can't create handler inside thread that has ...

  2. jenkins backup and migration

    service jenkins stop 压缩包:tar -czvf /tmp/xx.tar.gz --exclude=“workspace” --exclude=“.m2" --exclu ...

  3. 关于.NET中FileSystemWatcher的一些不被人注意的细节

    .NET 中的FileSystemWatcher可以监控文件系统中的更改.新建.删除和重命名,关于它的事件及属性的讨论有许多,但细节性的具体在什么情况下触发这些事件讨论不多.根据个人测试,总结如下: ...

  4. C 标准库 - string.h之memcmp使用

    memcmp Compare two blocks of memory. Compares the first num bytes of the block of memory pointed by ...

  5. Cocos文档案例游戏设计的梳理与分析

    导语:这是一篇新手教程,适用于已看完Cocos官方文档,但还对游戏设计.运行流程不熟悉的新人.这篇教程是对文档[快速上手]里那款名叫"摘星星"的坑爹小游戏(文档原话)流程的梳理,以 ...

  6. Ubuntu 安装 Caffe

    Caffe Caffe 安装(Python2 CPU版本) 参考博文https://blog.csdn.net/pangyunsheng/article/details/79418896 安装环境 U ...

  7. i.mx6 Android6.0.1分析input子系统:测试

    getevent与sendevent工具 Android系统提供了getevent与sendevent两个工具供开发者从设备节点中直接读取输入事件或写入输入事件. 在这里,我们测试音量加和音量减按键 ...

  8. 从零开始学JAVA(08)-使用SpringMVC4 Restful 风格引用静态文件 css/js/png

    在写完helloworld后想给网页加点样式(*.css),结果怎么也显示不了,百度了很多种方法后试行尝试,试验成功并记录下来,方便以后查看. 时隔两年,继续学习JAVA,太久没学了,忘记得差不多,还 ...

  9. Cannot find module 'rxjs/operators/share'

    描述: ionic项目,在使用了ngx-translate之后,项目编译完成,运行到浏览器时,出现如下错误: 其中ngx-translate参照官网最新教程使用,并且也尝试了angular4.3之前的 ...

  10. 第5章 scrapy爬取知名问答网站

    第五章感觉是第四章的练习项目,无非就是多了一个模拟登录. 不分小节记录了,直接上知识点,可能比较乱. 1.常见的httpcode: 2.怎么找post参数? 先找到登录的页面,打开firebug,输入 ...