Java:File和IO流

18 File(18-19)

IO:对硬盘的文件进行读写
File:对(文件/文件夹)进行创建,删除等,表示要读写的(文件/文件夹)在哪

18.1 File构造方法

方法名说明
File​(String pathname)通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例
File​(String parent, String child)从父路径名字符串和子路径名字符串创建新的 File实例
File​(File parent, String child)从父抽象路径名和子路径名字符串创建新的 File实例
public static void main(String[] args) {
//      将字符串转换为抽象路径名来创建新的File实例
        String path = "D:\\atest\\a.txt"; // D:\atest\a.txt
        File file = new File(path);
        System.out.println(file);
        
//      拼接两个字符串为一个抽象路径名
        String path1 = "D:\\atest";
        String path2 = "b.txt";
        File file1 = new File(path1,path2);//把两个路径拼接.
        System.out.println(file1);//D:\atest\b.txt

//      拼接 file + 字符串为一个抽象路径名
        File file2 = new File("D:\\atest");
        String path3 = "c.txt";
        File file3 = new File(file2,path3);
        System.out.println(file3); //D:\atest\c.txt
    }

18.2 绝对路径和相对路径

绝对路径:从盘符开始
相对路径:相对当前项目下的路径

public static void main(String[] args) {
        //这个路径固定不变了.
       File file = new File("D:\\itheima\\a.txt");

       //当前项目下的a.txt
       File file2 = new File("a.txt");

       //当前项目下 --- 指定模块下的 a.txt
       File file3 = new File("filemodule\\a.txt");
    }

18.3 File成员方法

创建文件/文件夹

方法名说明
public boolean createNewFile()创建一个新的空的文件
public boolean mkdirs()创建一个单级/多级文件夹
public static void main(String[] args) throws IOException {
        File file = new File("D:\\atest\\c.txt");
//      createNewFile 注意:
//        1.文件夹路径必需存在,如果不存在,例如atest没有提前创建,就会报错
//        2.a.txt不存在,可以创建成功,返回true。如果已存在,创建失败,返回false(不光判断文件,还是跟文件夹名判断)
//        3.不管有没有后缀名,只能创建文件.不会创建文件夹。

//        boolean res = file.createNewFile();
//        System.out.println(res);

//      mkdirs 注意点:
//        1.可以创建单级文件夹,也可以创建多级文件夹
//        2.不管调用者有没有后缀名,只能创建文件夹
//        3.如果文件名跟最后的路径名重复,也是会创建失败的,返回false。createNewFile也一样
        boolean res2 = file.mkdirs();
        System.out.println(res2);
    }

删除文件/文件夹

方法名说明
public boolean delete​()删除由此抽象路径名表示的文件或目录
public static void main(String[] args) {
        //删除文件
        File file = new File("D:\\atest\\a.txt");
        boolean res = file.delete();
        System.out.println(res);
//         删除空白文件夹
        File file2 = new File("D:\\atest\\b");
        boolean res2 = file2.delete();
        System.out.println(res2);
        // 删除带文件的文件夹
        File file3 = new File("D:\\atest\\c");
        boolean res3 = file3.delete(); // false
        System.out.println(res3);

        // 删除带空白文件夹的文件夹
        File file4 = new File("D:\\atest\\c");
        boolean res4 = file4.delete(); // false
        System.out.println(res4);
    }

判断和获取功能

方法名说明
public boolean isDirectory()测试此抽象路径名表示的File是否为目录
public boolean isFile()测试此抽象路径名表示的File是否为文件
public boolean exists()测试此抽象路径名表示的File(文件/文件夹)是否存在
public String getAbsolutePath()返回此抽象路径名的绝对路径名字符串
public String getPath()将此抽象路径名转换为路径名字符串
public String getName()返回由此抽象路径名表示的文件(文件+后缀格式)或目录的名称
public File[] listFiles()返回此抽象路径名表示的目录中的文件和目录的File对象数组

listFiles 方法注意事项

  1. 当调用者不存在时,返回null
  2. 当调用者是一个文件时,返回null
  3. 当调用者是一个空文件夹时,返回一个长度为0的数组
  4. 当调用者是一个有内容的文件夹时,将里面所有文件和文件夹的路径放在File数组中返回
  5. 当调用者是一个有隐藏文件的文件夹时,将里面所有文件和文件夹的路径放在File数组中返回,包含隐藏内容
  6. 当调用者是一个需要权限才能进入的文件夹时,返回null
public static void main(String[] args) {
        File file = new File("D:\\atest");
        File[] files = file.listFiles(); //返回值是一个File类型的数组
        System.out.println(files.length);
        for (File path : files) {
            System.out.println(path);
        }
    }

18.4 实用方法

删除带文件的文件夹

public class Test2 {
    public static void main(String[] args) {
        //练习二:删除一个多级文件夹
        //delete方法
        //只能删除文件和空文件夹.
        //如果现在要删除一个有内容的文件夹?
        //先删掉这个文件夹里面所有的内容.
        //最后再删除这个文件夹

        File src = new File("C:\\Users\\apple\\Desktop\\src");
        deleteDir(src);

    }

    private static void deleteDir(File src) {
        //先删掉这个文件夹里面所有的内容.
        //1.进入 --- 得到src文件夹里面所有内容的File对象.
        File[] files = src.listFiles();
        //2.遍历 --- 因为我想得到src文件夹里面每一个文件和文件夹的File对象.
        for (File file : files) {
            if(file.isFile()){
                //3.判断 --- 如果遍历到的File对象是一个文件,那么直接删除
                file.delete();
            }else{
                //4.判断
                //递归
                deleteDir(file);//参数一定要是src文件夹里面的文件夹File对象
            }
        }
        //最后再删除这个文件夹
        src.delete();
    }

}

统计文件类型的个数

public class Test3 {
    public static void main(String[] args) {
        //统计一个文件夹中,每种文件出现的次数.
        //统计 --- 定义一个变量用来统计. ---- 弊端:同时只能统计一种文件
        //利用map集合进行数据统计,键 --- 文件后缀名  值 ----  次数

        File file = new File("filemodule"); // filemodule模块名
        HashMap<String, Integer> hm = new HashMap<>();
        getCount(hm, file);
        System.out.println(hm);
    }

    private static void getCount(HashMap<String, Integer> hm, File file) {
        File[] files = file.listFiles();
        for (File f : files) {
            if(f.isFile()){
                String fileName = f.getName();
                String[] fileNameArr = fileName.split("\\.");
                if(fileNameArr.length == 2){
                    String fileEndName = fileNameArr[1];
                    if(hm.containsKey(fileEndName)){
                        //已经存在
                        //将已经出现的次数获取出来
                        Integer count = hm.get(fileEndName);
                        //这种文件又出现了一次.
                        count++;
                        //把已经出现的次数给覆盖掉.
                        hm.put(fileEndName,count);
                    }else{
                        //不存在
                        //表示当前文件是第一次出现
                        hm.put(fileEndName,1);
                    }
                }
            }else{
                getCount(hm,f);
            }
        }

    }
}

19 IO

19.1 概念

I 表示intput,是数据从硬盘进内存的过程,称之为
O 表示output,是数据从内存到硬盘的过程。称之为

所谓的读写,都是以内存为参照物,内存在读,内存在写。

io流读/写数据
在这里插入图片描述

读数据:内存读硬盘里的数据(输入流
写数据:内存把数据写到硬盘中(输出流

io流分类
在这里插入图片描述

19.2 字节流

1. 写数据

1.创建字节输出流对象

  • 注意点:
    如果文件不存在,会帮我们自动创建出来.
    如果文件存在,会把文件清空.

2.写数据

  • void write​(int b): 一次写一个字节数据
  • void write​(byte[] b): 一次写一个字节数组数据
  • void write​(byte[] b, int off, int len): 一次写一个字节数组的部分数据

3.释放资源

public static void main(String[] args) throws IOException {
        //1.创建字节输出流的对象 --- 告诉虚拟机我要往哪个文件中写数据了
        // 第二个参数就是续写开关,如果没有传递,默认就是false,
        // 如果第二个参数为true,表示打开续写功能,那么创建对象的这行代码不会清空文件.
        FileOutputStream fos = new FileOutputStream("D:\\a.txt",true);
        //FileOutputStream fos = new FileOutputStream(new File("D:\\a.txt")); // 两个效果一样

        //2,写数据
        // 这里的整数,实际写出的是整数在码表上对应的字母。
        fos.write(97); // 写入的是字节数据
        fos.write("\r\n".getBytes()); // 换行
        byte [] bys = {97,98,99,100,101,102,103};
        fos.write(bys,1,2); // 写入多个字节,可以传byte数组,也可以写多个值

        //3,释放资源
        fos.close();

    }

字节流写数据的异常处理(try…catch…finally)

public static void main(String[] args) {
        FileOutputStream fos = null;
        try {
            //System.out.println(2/0);
            fos = new FileOutputStream("D:\\a.txt");
            fos.write(97);
        }catch(IOException e){
           e.printStackTrace();
        }finally {
            //finally语句里面的代码,一定会被执行.
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

2. 读数据

1.创建字节输入流对象

  • 注意点:
    如果文件不存在,会直接报错.

2.读数据

  • public int read​(): 一次读取一个字节,返回值就是本次读到的那个字节数据.
  • public int read​(byte[] b):从输入流读取最多b.length个字节的数据

3.释放资源

基本使用

public static void main(String[] args) throws IOException {
        //如果文件存在,那么就不会报错.
        //如果文件不存在,那么就直接报错.
        FileInputStream fis = new FileInputStream("bytestream\\a.txt");

        int read = fis.read();
        //一次读取一个字节,返回值就是本次读到的那个字节数据.
        //也就是字符在码表中对应的那个数字.
        //如果我们想要看到的是字符数据,那么一定要强转成char


        System.out.println((char)read);

        //释放资源
        fis.close();
    }

读多个字节

public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("bytestream\\a.txt");
        //1,文件中多个字节我怎么办?
        /*while(true){
            int i1 = fis.read();
            System.out.println(i1);
        }*/

        int b;
        while ((b = fis.read())!=-1){
            System.out.println((char) b);
        }
        fis.close();
    }

复制功能

public static void main(String[] args) throws IOException {
        //创建了字节输入流,准备读数据.
        FileInputStream fis = new FileInputStream("C:\\itheima\\a.avi");
        //创建了字节输出流,准备写数据.
        FileOutputStream fos = new FileOutputStream("bytestream\\a.avi");

        int b;
        while((b = fis.read())!=-1){
            fos.write(b);
        }

        fis.close();
        fos.close();
    }

提升复制速度的解决方案

public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("C:\\itheima\\a.avi");
        FileOutputStream fos = new FileOutputStream("bytestream\\a.avi");

        byte [] bytes = new byte[1024];
        int len; //本次读到的有效字节个数 -- 这次读了几个字节.

        while((len = fis.read(bytes))!=-1){
            fos.write(bytes,0,len);
        }

        fis.close();
        fos.close();
    }

19.3 字节缓冲流

主要是提升复制的效率

1. 复制数据(单个字节复制)

public static void method1() throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\itcast\\字节流复制图片.avi"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("myByteStream\\字节流复制图片.avi"));

        int by;
        while ((by=bis.read())!=-1) {
            bos.write(by);
        }

        bos.close();
        bis.close();
    }

2. 复制数据(字节数组复制)

public static void method2() throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\itcast\\字节流复制图片.avi"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("myByteStream\\字节流复制图片.avi"));

        byte[] bys = new byte[1024];
        int len;
        while ((len=bis.read(bys))!=-1) {
            bos.write(bys,0,len);
        }

        bos.close();
        bis.close();
    }

19.4 字符流

1. 编码表

重点:
windows默认使用码表为:GBK,一个字符个字节。
idea和以后工作默认使用UnicodeUTF-8编解码格式,一个中文个字节。

2. 编码和解码方法

编码

方法说明
byte[] getBytes​()使用平台的默认字符集将该 String编码为一系列字节,将结果存储到新的字节数组中
byte[] getBytes​(String charsetName)使用指定的字符集将该 String编码为一系列字节,将结果存储到新的字节数组中

解码

方法说明
String​(byte[] bytes)通过使用平台的默认字符集解码指定的字节数组来构造新的 String
String​(byte[] bytes, String charsetName)通过指定的字符集解码指定的字节数组来构造新的 String

3. 字符流

字符流 = 字节流 + 编码表
不管是在哪张码表中,中文的第一个字节一定是负数。

4. 字节流,字符流使用

1、想要进行拷贝,一律使用字节流或者字节缓冲流
2、想要把文件中的数据读到内存中打印或运算,请使用字符输入流
想要把集合,数组,键盘录入等数据写到文件中,请使用字符输出流
3、GBK码表一个中文个字节,UTF-8编码格式一个中文3个字节。

5. 写数据

1.创建字符输出流对象

  • 注意点:
    如果文件不存在,会帮我们自动创建出来.但是要保证父级路径存在。
    如果文件存在,会把文件清空.

2.写数据

  • 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): 一次写一个字符串的部分数据,可以写中文

3.释放资源

  • flush(): 刷新流,还可以继续写数据。刷新流:相当于把内存此时的内容保存到硬盘中。
  • close(): 关闭流,释放资源,但是在关闭之前会先刷新流。一旦关闭,就不能再写数据
public static void main(String[] args) throws IOException {
        //创建字符输出流的对象
        FileWriter fw = new FileWriter("charstream\\a.txt");

        fw.write(97);

        char [] chars = {98,99,100,101};
        fw.write(chars);

        fw.write(chars,0,3);

        fw.write("黑马程序员abc");
		//刷新流,把上面的内容先保存一份到硬盘中
		fw.flush();

        fw.write("黑马程序员abc",0,2);

        //释放资源
        fw.close();
    }

6. 读数据

public static void main(String[] args) throws IOException {
        //创建字符输入流的对象
       // FileReader fr = new FileReader(new File("charstream\\a.txt"));
        FileReader fr = new FileReader("charstream\\a.txt");


        //读取数据
        //一次读取一个字符
        int ch;
        while((ch = fr.read()) != -1){
            System.out.println((char) ch);
        }
		
		//创建一个数组
        char [] chars = new char[1024];
        int len;

        //read方法还是读取,但是是一次读取多个字符
        //他把读到的字符都存入到chars数组。
        //返回值:表示本次读到了多少个字符。
        while((len = fr.read(chars))!=-1){
            System.out.println(new String(chars,0,len));
        }

        //释放资源
        fr.close();
    }

19.5 字符缓冲流

1. 读数据

public static void main(String[] args) throws IOException {
        //字符缓冲输入流
        BufferedReader br = new BufferedReader(new FileReader("charstream\\a.txt"));

        //读取数据
        char [] chars = new char[1024];
        int len;
        while((len = br.read(chars)) != -1){
            System.out.println(new String(chars,0,len));
        }

        br.close();
    }

2. 写数据

public static void main(String[] args) throws IOException {
        //字符缓冲输出流
        BufferedWriter bw = new BufferedWriter(new FileWriter("charstream\\a.txt"));

        //写出数据
        //实际写出的是97对应的字符a
        bw.write(97);
        bw.write("\r\n");

        //实际写出的是97 - 101 对应的字符 abcde
        char [] chars = {97,98,99,100,101};
        bw.write(chars);
        bw.write("\r\n");

        //实际写的是abc
        bw.write(chars,0,3);
        bw.write("\r\n");

        //会把字符串的内容原样写出
        bw.write("黑马程序员");
        bw.write("\r\n");

        //会把字符串的一部分写出 abcde
        String line = "abcdefg";
        bw.write(line,0,5);

        bw.flush();

        bw.close();

    }

3. 字符缓冲流特有方法–newLine 跨平台的换行符

public static void main(String[] args) throws IOException {
        //字符缓冲流的特有功能
        //字符缓冲输出流BufferedWrite : newLine  跨平台的换行符


        //创建对象
        BufferedWriter bw = new BufferedWriter(new FileWriter("charstream\\a.txt"));


        //写出数据
        bw.write("黑马程序员666");
        //跨平台的回车换行
        bw.newLine();
        bw.write("abcdef");
        //跨平台的回车换行
        bw.newLine();
        bw.write("-------------");

        //刷新流
        bw.flush();
        //释放资源
        bw.close();
    }

4. 字符缓冲流特有方法–readLine 读一整行

public static void main(String[] args) throws IOException {
        //字符缓冲流的特有功能
        //字符缓冲输入流BufferedReader: readLine 读一整行

        //创建对象
        BufferedReader br = new BufferedReader(new FileReader("charstream\\a.txt"));


        //使用循环来进行改进
        String line;
        //可以读取一整行数据。一直读,读到回车换行为止。
        //但是他不会读取回车换行符。
        // readLine如果读不到数据返回null
        while((line = br.readLine()) != null){
            System.out.println(line);
        }

        //释放资源
        br.close();

    }

19.6 字节流和字符流小结

在这里插入图片描述

19.7 转换流

在这里插入图片描述

转换流就是来进行字节流和字符流之间转换的

  • InputStreamReader是从字节流到字符流的桥梁
  • OutputStreamWriter是从字符流到字节流的桥梁
public static void main(String[] args) throws IOException {
        //method1();
        //method2();

        //在JDK11之后,字符流新推出了一个构造,也可以指定编码表
        FileReader fr = new FileReader("C:\\Users\\apple\\Desktop\\a.txt", Charset.forName("gbk"));
        int ch;
        while ((ch = fr.read())!=-1){
            System.out.println((char) ch);
        }
        fr.close();
    }

    private static void method2() throws IOException {
        //如何解决乱码现象
        //文件是什么码表,那么咱们就必须使用什么码表去读取.
        //我们就要指定使用GBK码表去读取文件.
        InputStreamReader isr = new InputStreamReader(new FileInputStream("C:\\Users\\apple\\Desktop\\a.txt"),"gbk");
        int ch;
        while((ch = isr.read())!=-1){
            System.out.println((char) ch);
        }
        isr.close();


        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("C:\\Users\\apple\\Desktop\\b.txt"),"UTF-8");
        osw.write("我爱学习,谁也别打扰我");
        osw.close();
    }

    //这个方法直接读取会产生乱码
    //因为文件是GBK码表
    //而idea默认的是UTF-8编码格式.
    //所以两者不一致,导致乱码
    private static void method1() throws IOException {
        FileReader fr = new FileReader("C:\\Users\\apple\\Desktop\\a.txt");
        int ch;
        while ((ch = fr.read())!=-1){
            System.out.println((char) ch);
        }
        fr.close();
    }

19.8 对象操作流

可以把对象以字节的形式写到本地文件,直接打开文件,是读不懂的,需要再次用对象操作流读到内存中。

对象操作流分为两类:对象操作输入流对象操作输出流

  • 对象操作输出流(对象序列化流):就是将对象写到本地文件中,或者在网络中传输对象
  • 对象操作输入流(对象反序列化流):把写到本地文件中的对象读到内存中,或者接收网络中传输的对象

1. 读数据

public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt"));
        User o = (User) ois.readObject();
        System.out.println(o);
        ois.close();

    }

2. 写数据

public static void main(String[] args) throws IOException {
        User user = new User("zhangsan","qwer");

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"));
        oos.writeObject(user);
        oos.close();
    }

// 如果想要这个类的对象能被序列化,那么这个类必须要实现一个接口.Serializable
// User类
public class User implements Serializable {}

3. 注意:序列化对象后,修改了对象所属的JavaBean类

问题:此时会出问题,会抛出InvalidClassException异常.
解决:给对象所属的加一个serialVersionUID
技巧:打开ArrayList源码,它也实现了Serializable 接口

public class User implements Serializable {
    //serialVersionUID 序列号
    //如果我们自己没有定义,那么虚拟机会根据类中的信息会自动的计算出一个序列号.
    //问题:如果我们修改了类中的信息.那么虚拟机会再次计算出一个序列号.

    //第一步:把User对象序列化到本地. --- -5824992206458892149
    //第二步:修改了javabean类. 导致 --- 类中的序列号 4900133124572371851
    //第三步:把文件中的对象读到内存. 本地中的序列号和类中的序列号不一致了.

    //解决?
    //不让虚拟机帮我们自动计算,我们自己手动给出.而且这个值不要变.

    private static final long serialVersionUID = 1L;

    // ...其他代码省略

}

4. 注意:成员不想序列化处理

给该成员变量加transient关键字修饰,该关键字标记的成员变量不参与序列化过程

public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    private String username;
    private transient String password;

}

5. 循环获取对象操作流

方式一

public static void main(String[] args) throws IOException, ClassNotFoundException {
        Student s1 = new Student("杜子腾",16);
        Student s2 = new Student("张三",23);
        Student s3 = new Student("李四",24);

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"));
        oos.writeObject(s1);
        oos.writeObject(s2);
        oos.writeObject(s3);

        oos.close();


        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt"));
        Object obj;
        // 对象操作流,不是null,也不是-1,而是一个异常错误
      /*  while((obj = ois.readObject()) != null){
            System.out.println(obj);
        }*/

       while(true){
           try {
               Object o = ois.readObject();
               System.out.println(o);
           } catch (EOFException e) {
               break;
           }
       }
        ois.close();
    }

方式二

public static void main(String[] args) throws IOException, ClassNotFoundException {
        Student s1 = new Student("杜子腾",16);
        Student s2 = new Student("张三",23);
        Student s3 = new Student("李四",24);

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"));
        ArrayList<Student> list = new ArrayList<>();
        list.add(s1);
        list.add(s2);
        list.add(s3);

        //我们往本地文件中写的就是一个集合
        oos.writeObject(list);

        oos.close();


        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt"));
        ArrayList<Student> list2 = (ArrayList<Student>) ois.readObject();
        for (Student student : list2) {
            System.out.println(student);
        }
        ois.close();
    }

19.9 Properties

1. Properties概述

  • 是一个Map体系的集合类
  • Properties中有跟IO相关的方法
  • 只存字符串

2. Properties集合的增删改查

public static void main(String[] args) {
        Properties prop = new Properties();
        //增
        prop.put("小龙女","尹志平");
        prop.put("郭襄","杨过");
        prop.put("黄蓉","欧阳克");
        System.out.println(prop);

        //删
        //prop.remove("郭襄");
        //System.out.println(prop);

        //改
        //put --- 如果键不存在,那么就添加,如果键存在,那么就覆盖.
        prop.put("小龙女","杨过");
        System.out.println(prop);
        //查

        //Object value = prop.get("黄蓉");
        //System.out.println(value);

        //遍历
        Set<Object> keys = prop.keySet();
        for (Object key : keys) {
            Object value = prop.get(key);
            System.out.println(key + "=" + value);
        }

        System.out.println("========================");

        //装的是所有的键值对对象.
        Set<Map.Entry<Object, Object>> entries = prop.entrySet();
        for (Map.Entry<Object, Object> entry : entries) {
            Object key = entry.getKey();
            Object value = entry.getValue();
            System.out.println(key + "=" + value);
        }

    }

3. Properties集合的特有方法

public static void main(String[] args) {
        //Object setProperty​(String key, String value) --- put替代
                    //设置集合的键和值,都是String类型,底层调用 Hashtable方法 put
        Properties prop = new Properties();
        prop.setProperty("江苏","南京");
        prop.setProperty("安徽","南京");
        prop.setProperty("山东","济南");

        System.out.println(prop);
        //String getProperty​(String key)  --- get替代
                    //使用此属性列表中指定的键搜索属性

       /* String value = prop.getProperty("江苏");
        System.out.println(value);*/


        //Set<String> stringPropertyNames​()  --- keySet替代
                    //从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串

        Set<String> keys = prop.stringPropertyNames();
        for (String key : keys) {
            String value = prop.getProperty(key);
            System.out.println(key + "=" + value);
        }

    }

4. Properties集合与IO有关方法–load

public static void main(String[] args) throws IOException {
        //void load​(Reader reader)                     将本地文件中的键值对数据读取到集合中
        //void store​(Writer writer, String comments)   将集合中的数据以键值对形式保存在本地


        //读取
        Properties prop = new Properties();
        FileReader fr = new FileReader("prop.properties");
        //调用完了load方法之后,文件中的键值对数据已经在集合中了.
        prop.load(fr);
        fr.close();

        System.out.println(prop);
    }
}

5. Properties集合与IO有关方法–store

public static void main(String[] args) throws IOException {
        //void load​(Reader reader)                     将本地文件中的键值对数据读取到集合中
        //void store​(Writer writer, String comments)   将集合中的数据以键值对形式保存在本地


        Properties prop = new Properties();
        prop.put("zhangsan","123");
        prop.put("lisi","456");
        prop.put("wangwu","789");

        FileWriter fw = new FileWriter("prop.properties");
        prop.store(fw,null);
        fw.close();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值