IO流--与properties集合配合使用

IO流--与properties集合配合使用:

注:生产上主要用于常量文件的配置,读取常量文件;

1:properties集合的放值与取值:

 /*
     *  properties集合继承自hashTable,使用properties父类的放值(put();),取值(get();)
     *  功能,遍历集合得到的是Object类型的;
     *  所以我们使用properties自己特有的放值(setProperties();)和取值(getProperties();)的功能
     *  遍历集合得到的是String类型的;
     */
    @Test
    public void test() throws IOException {
        Properties properties = new Properties();
        properties.setProperty("张三","23");
        properties.setProperty("李四","25");
        properties.setProperty("王二","29");
        //properties父类的遍历:
//        Set<Object> objects = properties.keySet();
//        for(Object key:objects){
//            Object value = properties.get(key);
//            System.out.println(key +"="+value);
//        }
        //使用自身的方法遍历:
        Set<String> strings = properties.stringPropertyNames();
             for(String key :strings){
                 String value = properties.getProperty(key);
                 System.out.println(key+"="+value);
             }
    }

2:从properties集合写入参数到文件:

 public void propertiesWrite() throws IOException {
        Properties properties = new Properties();
        properties.setProperty("张三","23");
        properties.setProperty("李四","25");
        properties.setProperty("王二","29");
        Writer writer = new FileWriter("OnlyFileTest/properties.txt");
        properties.store(writer,"文件说明(注释)");
        writer.close();
    }

3:从文件中读取键值对的参数到properties集合中:

 public void propertiesRead() throws IOException {
        Reader fileReader = new FileReader("OnlyFileTest/properties.txt");
        Properties properties = new Properties();
        properties.load(fileReader);
        fileReader.close();
        System.out.println(properties);
    }

原文地址:https://www.cnblogs.com/dw3306/p/9459550.html

时间: 2024-08-30 00:18:14

IO流--与properties集合配合使用的相关文章

Java 中与IO流有关的集合(Properties集合)

属性集 java.util.Properties继承于Hashtable,来表示一个持久的属性集.它使用键值结构存储数据,每个键及其对应值都是一个字符串.该类也被许多Java类使用,比如获取系统属性时, System.getProperties方法就是返回一个Properties对象. Properties类 Properties集合是Hashtable集合的子类,Hashtable类是Map接口的其中一个实现类.Properties集合 是唯一一个和IO流相结合的集合.Properties类表

java学习--基础知识进阶第十天--标准输入流 &amp; 转换流 &amp; 打印流、对象操作流 、Properties集合

今日内容介绍 u  标准输入流 & 转换流 & 打印流 u  对象操作流 u  Properties集合 第1章 标准输入流 & 转换流 & 打印流 1.1 标准输入输出流 public static final InputStream in:标准输入流 public static final PrintStream out:标准输出流 package com.itheima_05; /* * 标准输入输出流: public static final InputStream

IO流之Properties类

Properties类介绍 Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字符串. 特点: 1.Hashtable的子类,map集合中的方法都可以用. 2.该集合没有泛型.键值都是字符串. 3.它是一个可以持久化的属性集.键值可以存储到集合中,也可以存储到持久化的设备(硬盘.U盘.光盘)上.键值的来源也可以是持久化的设备. 4.有和流技术相结合的方法. l  load(InputStream)  把指定流所对应的

转换流,Properties 集合

转换流: OutputStreamWriter  写入转码 字符流通向字节流的桥梁,可使用指定的字符编码表,将要写入流中的字符编码成字节. 格式: //明确目的地 FileOutputStream fos=new FileOutputStream("D:\\text\\c.txt",true); // OutputStreamWriter osw=new OutputStreamWriter(fos,"utf-8"); osw.write("你好"

IO流之Properties(String键值对)+序列流+打印流+commons-IO(最终流程)

学了这么多全是给他用的之Commons 用这个的前提需要导包, ①创建lib文件夹 ②将下载的commos-io.jar拷贝到lib文件夹 ③右键点击commons-io.jar,Build Path→Add to Build Path 然后介绍两个工具类 1.2 FilenameUtils 这个工具类是用来处理文件名,他可以轻松解决不同操作系统文件名称规范不同的问题 l 常用方法: getExtension(String path):获取文件的扩展名: getName(String filen

Java基础学习笔记二十 IO流

转换流 在学习字符流(FileReader.FileWriter)的时候,其中说如果需要指定编码和缓冲区大小时,可以在字节流的基础上,构造一个InputStreamReader或者OutputStreamWriter,这又是什么意思呢? OutputStreamWriter类 查阅OutputStreamWriter的API介绍,OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的字符编码表,将要写入流中的字符编码成字节.它的作用的就是,将字符串按照指定的编码表转成字节,

Properties集合。

Properties: java.util.Properties extends Hashtable<k,v> implements Map<k,v> Properties类表示了一个持久的属性集,是唯一和IO流相结合的集合. 可以用其store方法,把集合中的临时数据,持久化写到硬盘中. 可以用其load方法,把硬盘中保存的文件(键值对),读取到集合中使用. Properties集合的key和value默认是字符串. Properties的一些特有方法: Object setPr

java的Io流机制的学习

IO流机制 File类的使用 File类的构造方法 File(URI?uri) File(String?pathname) File(File?parent, String?child) File(String?parent, String?child) File类的常用方法 boolean exists():判断文件是否存在 boolean createNewFile() :创建一个新文件,只能创建一个文件,不能创建目录(文件夹),创建时先判断文件 是否存在 ,不存在则创建并返回true, 存

JAVA学习第五十三课 — IO流(七)File对象练习 &amp; Properties集合

一.练习 深度遍历文件夹 深度遍历很自然而然想到递归,而递归就很自然的想到其实现的底层算法是栈 对指定目录下列出所有内容(包含子目录的内容) PS:建议不要遍历C盘 import java.io.*; public class Main { public static void main(String[] args) throws IOException { File dir = new File("D:\\ACM集训"); ListAllDemo(dir,0); } public s