Java学习与复习笔记--Properties集合&缓冲流

使用Properties集合存储数据,遍历:

/*
* java.util.Properties 集合 extends HashTable<k,v> implements Map<k,v>
* Properties类表示一组持久的属性。 Properties可以保存到流中或从流中加载。
* Properties集合是一个唯一和IO流相结合的集合。
* 可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储。
* 可以使用Properties集合中的方法load,把硬盘中保存的文件(键值对),读取到集合中使用
*
* 属性列表中的每个键及其对应的值都是一个字符串。
* Properties集合是一个双列集合,key和value默认都是字符串
* */
public class Demo01Properties {
    public static void main(String[] args) {
        show01();
    }
    /*
    * 使用Properties集合存储数据,遍历取出Properties集合中的数据
    * Properties集合是一个双列集合,key和value默认都是字符串
    * Properties集合有一些操作字符串的特有方法
    * Object setProperty(String key, String value) :调用 Hashtable方法 put 。
    * String getProperty(String key) :通过key,找到value值,此方法相当于Map集合中的get(key)方法
    * Set<String> stringPropertyNames() :返回此属性列表中的一组键,其中键及其对应的值为字符串,此方法相当于Map集合中的KeySet方法
    * */
    private static void show01() {
        //创建Properties集合对象
        Properties prop=new Properties();
        //使用SetProperty往集合中添加数据
        prop.setProperty("赵丽颖","168");
        prop.setProperty("迪丽热巴","165");
        prop.setProperty("古力娜扎","160");
        //prop.put(1,true);
        //使用stringPropertyNames()把Properties集合中的键取出,存储到一个Set集合中
        Set<String> set = prop.stringPropertyNames();
        //遍历Set集合,取出Properties集合中的每一个键
        for (String s : set) {
            //使用getProperty方法通过key获取value。
            String value = prop.getProperty(s);
            System.out.println(s+"="+value);
        }
    }
}

Properties集合中的方法-store

/*
* 可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储。
* void store(OutputStream out, String comments)
* void store(Writer writer, String comments)
* 参数:OutputStream out:字节输出流,不能写入中文
*       Writer writer:字符输出流,可以写中文
*       String comments:注释,用来解释说明保存的文件做什么用的,不能使用中文,会产生乱码,默认是Unicode编码
*       一般使用“空字符串”
*    使用步骤:
*    1.创建Properties集合对象,添加数据
*    2.创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地
*    3.使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
*    4.释放资源
**/
private static void show02() throws IOException{
    Properties prop=new Properties();
    prop.setProperty("赵丽颖","168");
    prop.setProperty("迪丽热巴","165");
    prop.setProperty("古力娜扎","160");
    /*FileWriter fw=new FileWriter("FileAndIO\\prop.txt");
    prop.store(fw,"save data");
    fw.close();*/
    //字节流
    prop.store(new FileOutputStream("FileAndIO\\prop2.txt"),"");
}

Properties集合中的方法load:

/*
* 可以使用Properties集合中的方法load,把硬盘中保存的文件(键值对),读取到集合中使用
* void load(InputStream inStream)
* void load(Reader reader)
* 参数:InputStream inStream:字节输入流,不能读取含有中文的键值对
*       Reader reader:字符输入流,能读取含有中文的键值对
*   使用步骤:
*   1.创建Properties集合对象
*   2.使用Properties集合对象中的方法load读取保存键值对的文件
*   3.遍历Properties集合
*   注意事项:
*   1.存储键值对的文件中,键值对的连接符号可以使用=,空格(其他符号)
*   2.存储键值对的文件中,可以使用#进行注释,被注释的键值对不会再被读取
*   3.存储键值对的文件中,键与值默认都是字符串,不用再加引号
*   */
private static void show03() throws IOException {
    Properties prop=new Properties();
    //prop.load(new FileReader("FileAndIO\\prop.txt"));
    prop.load(new FileInputStream("FileAndIO\\prop.txt"));//出现问题乱码
    //遍历Properties集合
    Set<String> set = prop.stringPropertyNames();
    for (String key : set) {
        String value = prop.getProperty(key);
        System.out.println(key+"="+value);
    }
}

缓冲流:

字节缓冲输入流,给基本的字节输入流增加一个缓冲区(数组),提高基本的字节输入流的读取效率。

BufferedInputStream(new FileInputStream())

BufferedOutputStream_字节缓冲:

/*
* java.io.BufferedOutputStream extends OutputStream
* BufferedOutputStream:字节缓冲输出流
* 继承自父类的共性成员方法:
*  void close() 关闭此输出流并释放与此流相关联的任何系统资源。
 * void flush() 刷新此输出流并强制任何缓冲的输出字节被写出。
 * void write(byte[] b) 将 b.length字节从指定的字节数组写入此输出流。
 * void write(byte[] b, int off, int len) 从指定的字节数组写入 len个字节,从偏移 off开始输出到此输出流。
 * abstract void write(int b) 将指定的字节写入此输出流。
 * 构造方法:
 * BufferedOutputStream(OutputStream out) 创建一个新的缓冲输出流,以将数据写入指定的底层输出流
 * BufferedOutputStream(OutputStream out, int size) 创建一个新的缓冲输出流,以便以指定的缓冲区大小将数据写入指定的底层输出流。
 * 参数:OutputStream out:字节输出流
 *  我们可以传递FileOutputStream,缓冲流会给FileOutputStream增加一个缓冲区,提高FileOutputStream的写入效率
 *  int size:指定缓冲流内部缓冲区大小,不指定默认
 * 使用步骤(重点):
 * 1.创建一个FileOutputStream对象,构造方法中绑定要输出的目的地
 * 2.创建BufferedOutputStream对象,构造方法中传递FileOutputStream对象,提高FileOutputStream对象效率
 * 3.使用BufferedOutputStream对象中的方法write,把数据写入到内部的缓冲区中
 * 4.使用BufferedOutputStream对象中的方法flush,把内部缓冲区中的数据,刷新到文件中
 * 5.释放资源(会先调用flush刷新数据,第四步可以省略)
 * */
public class Demo01BufferedOutputStream {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos=new FileOutputStream("IO\\a.txt");
        BufferedOutputStream bos=new BufferedOutputStream(fos);
        bos.write("我把数据写入到内部缓冲区中".getBytes());
        //bos.flush();
        bos.close();
    }
}

BufferedIutputStream_字节缓冲:

/*
* java.io.BufferedInputStream extends InputStream
* BufferedInputStream:字节缓冲输入流
* 继承自父类的方法:
* int read() :从输入流读取数据的下一个字节。
* int read(byte[] b) :从输入流读取一些字节数,并将它们存储到缓冲区 b 。
* void close() :关闭此输入流并释放与流相关联的任何系统资源
* 构造方法:
* BufferedInputStream(InputStream in) :创建一个 BufferedInputStream并保存其参数,输入流 in ,供以后使用
* BufferedInputStream(InputStream in, int size) :创建 BufferedInputStream具有指定缓冲区大小,并保存其参数,输入流 in ,供以后使用。
* 参数:
*   InputStream in:字节输入流
 *   我们可以传递FileInputStream,缓冲流会给FileInputStream增加一个缓冲区,提高FileInputStream的读取效率
 *   int size:指定缓冲流内部缓冲区的大小,不指定默认
 * 使用步骤(重点):
 *   1.创建FileInputStream对象,构造方法中要绑定读取的数据源
 *   2.创建BufferedInputStream对象,构造方法中传递FileInputStream对象,提高FileInputStream对象的读取效率
 *   3.使用BufferedInputStream对象中的方法read,读取文件
 *   4.释放资源*/
public class Demo02BufferedInputStream {
    public static void main(String[] args) throws IOException {
        FileInputStream fis=new FileInputStream("IO\\a.txt");
        BufferedInputStream bis=new BufferedInputStream(fis);
        int len = 0;//记录每次读取到的字节
        while((len=bis.read())!=-1){
            System.out.println(len);
        }
        bis.close();
    }
}

缓冲流的效率测试_复制文件:

/*
* 文件复制的步骤:
* 1.创建字节缓冲输入流对象,构造方法中传递字节输入流
* 2.创建字节缓冲输出流对象,构造方法中传递字节输出流
* 3.使用字节缓冲输入流对象中的方法read,读取文件
* 4.使用字节缓冲输出流中的方法write,把读取的数据写入到内部缓存区中
* 5.释放资源(会先把缓冲区中的数据,刷新到文件中)*/
public class Demo04FileCopy {
    public static void main(String[] args) throws IOException {
        long s=System.currentTimeMillis();
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream("FileAndIO\\b.txt"));
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("d:\\c.txt"));
        /*int len=0;
        while((len=bis.read())!=-1){
            bos.write(len);
        }*/
        byte[] bytes=new byte[1024];
        int len=0;//每次读取的有效字节个数
        while((len=bis.read(bytes))!=-1){
            bos.write(bytes,0,len);
        }
        bos.close();
        bis.close();
        long e=System.currentTimeMillis();
        System.out.println("复制文件共耗时:"+(e-s)+"毫秒");
    }
}

BufferedWriter_字符缓冲输出流:

/*
* java.io.BufferedWriter extends Writer
* BufferedWriter:字符缓冲输出流
* 继承自父类的共性成员方法:
* void write(int c) 写一个字符
 * void write(char[] cbuf) 写入一个字符数组。
 * void write(char[] cbuf, int off, int len) 写入字符数组的一部分。
 * void write(String str) 写一个字符串
 * void write(String str, int off, int len) 写一个字符串的一部分。
 * void flush() 刷新流
 * void close() 关闭流,先刷新。
 * 构造方法:
 * BufferedWriter(Writer out) 创建使用默认大小的输出缓冲区的缓冲字符输出流。
 * BufferedWriter(Writer out, int sz) 创建一个新的缓冲字符输出流,使用给定大小的输出缓冲区。
 * 参数:
 * Writer out:字符输出流
 * 我们可以传递FileWriter,缓冲流会给FileWriter增加一个缓冲区,提高FileWriter的写入效率
 * int sz:指定缓冲区大小,不写默认大小
 * 特有的成员方法:
 *    void newLine() 写一行行分隔符。 会根据不同的操作系统,获取不同的行分隔符
 *   使用步骤:
 *   1.创建字符缓冲输出流对象,构造方法中传递字符输出流
 *   2.调用字符缓冲输出流中的方法write,把数据写入到内存缓冲区中
 *   3.调用字符缓冲输出流中的方法flush,把内存缓冲区中的数据,刷新到文件中
 *   4.释放资源
 *   */
public class Demo05BufferedWriter {
    public static void main(String[] args) throws IOException {
        BufferedWriter bw=new BufferedWriter(new FileWriter("IO\\c.txt"));
        for (int i = 0; i <10 ; i++) {
            bw.write("你好啊");
            bw.newLine();//换行
        }
        bw.flush();
        bw.close();
    }
}

BufferedReader_字符缓冲输入流:

/*
* java.io.BufferedReader  extends Reader
* 继承自父类的共性成员方法:
* int read() 读取单个字符并返回。
* int read(char[] cbuf) :一次读取多个字符,将字符读入数组
* void close() :释放资源
* 构造方法:
* BufferedReader(Reader in) :创建使用默认大小的输入缓冲区的缓冲字符输入流。
* BufferedReader(Reader in, int sz) :创建使用指定大小的输入缓冲区的缓冲字符输入流。
* 参数:
* Reader in:字符输入流
* 我们可以传递FileReader,缓冲流会给FileReader增加一个缓冲区,提高FileReader的效率
* 特有的成员方法:
* String readLine() :读一行文字。读取一行数据
* 行的终止符号:通过下列字符之一即可认为某行已终止:换行(‘\n’)、回车('\r')或者回车后直接跟着换行(\r\n).
*  返回值:包含行的内容的字符串,不包括任何行终止字符,如果已达到流的末尾,则为null
*
*  使用步骤:
*  1.创建一个字符缓冲输入流对象,构造方法中传递字符输入流
*  2.使用字符缓冲输入流中的方法read或者readline读取文本
*  3.释放资源
 * */
public class Demo06BufferedReader {
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new FileReader("IO\\c.txt"));
        /*String line = br.readLine();
        System.out.println(line);
        line = br.readLine();
        System.out.println(line);
        line = br.readLine();
        System.out.println(line);*/
        //以上读取是一个重复的过程,所以可以使用循环优化
        //不知道文件中有多少行数据,所以使用while循环,while的结束条件,读取到null结束
        String line;
        while((line=br.readLine())!=null){
            System.out.println(line);
        }
        br.close();
    }
}

练习:文本排序

/*
* 练习:对文本的内容进行排序
* 按照(1,2,3...)顺序排序
* 分析:
* 1.创建一个HashMap集合对象,key:存储每行文本的序号(1,2,3...);value:存储每行的文本
* 2.创建字符缓冲输入流对象,构造方法中绑定字符输入流
* 3.创建字符缓冲输出流对象,构造方法中绑定字符输出流
* 4.使用字符缓冲输入流中的方法readLine,逐行读取文本
* 5.对读取到的文本进行切割,获取行中的序号和文本内容
* 6.把切割好的序号和文本的内容存储到HashMap集合中(key序号是有序的,会自动排序)
* 7.遍历HashMap集合,获取每一个键值对
* 8.把每一个键值对,拼接位一个文本行
* 9.把拼接好的文本,使用字符缓冲输出流中的write方法,写入到文件中
* 10.释放资源
* */
public class Demo07Practice {
    public static void main(String[] args) throws IOException {
        HashMap<String,String> map=new HashMap<>();
        BufferedReader br=new BufferedReader(new FileReader("IO\\c.txt"));
        BufferedWriter bw=new BufferedWriter(new FileWriter("IO\\a.txt"));
        String line;
        while((line=br.readLine())!=null){
            String[] arr = line.split("\\.");
            map.put(arr[0],arr[1]);
        }
        for (String key : map.keySet()) {
            String value = map.get(key);
            line=key+"."+value;
            bw.write(line);
            bw.newLine();//写换行
        }
        bw.close();
        br.close();
    }
}

其中遇到的问题:读取的文本中有一行空行,然后读取的时候在map.put(arr[0],arr[1]);出现了数组越界异常,把原文本中的空的一行删除后,程序可顺利运行,并且目标文件中的数据排序成功

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值