• Java 第二阶段提升编程能力【IO流】


    代码链接:https://download.csdn.net/download/qq_52354698/86404845?spm=1001.2014.3001.5501

    1. 文件流

    文件在程序中是以流的形式来操作的
    在这里插入图片描述
    流:数据在数据源(文件)和程序(内存)之间经历的路径
    输入流:数据从数据源(文件)到程序(内存)的路径
    输出流:数据从程序(内存)到数据源(文件)的路径

    2. 常用的文件操作

    1. 创建文件对象相关构造器和方法

    new File(String pathname)//根据路径创建一个File对象
    new File(File parent, String child)//根据父目录文件+子路径构建
    new File(String parent,String child)//根据父目录+子路径构建
    
    • 1
    • 2
    • 3

    2. 获取文件的相关信息

    getName();//得到文件名字
    getAbsolutePath();//得到文件绝对路径
    getParent();//得到文件父级目录
    length();//得到文件大小(字节)
    exists();//检查文件是否存在
    isFile();//检查是否是一个文件
    isDirectory();//检查是否是一个目录
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3. 目录的操作和文件删除

    mkdir();//创建一级目录
    mkdirs();//创建多级目录
    delete();//删除空目录或文件
    
    • 1
    • 2
    • 3

    3. IO流原理及流的分类

    1. Java IO流原理

    1. I/O 是 Input/Output 的缩写,I/O 技术是非常实用的技术,用于处理数据传输。读写文件、网络通讯。
    2. Java 程序中,对于数据的输入、输出操作以 “流(stream)” 的形式进行。
    3. java.io 包下提供了各种 “流” 类和接口,用以获取不同种类的数据,并通过方法输入或输出数据。
    4. 输入 input:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中。
    5. 输出 output:将程序(内存)数据输出到磁盘、光盘等存储设备中。

    在这里插入图片描述

    2. 流的分类

    • 按操作数据单位不同分为:字节流(8bit)【二进制文件】、字符流(按字符)【文本文件】
    • 按数据流的流向不同分为:输入流、输出流
    • 按流的角色的不同分为:节点流,处理流,包装流

    在这里插入图片描述

    1. Java 的 IO 流共涉及 40 多个类,实际上非常规则,都欧式从如上4个抽象基本类培生的。
    2. 由这四个类派生出来的子类名称都是以其父类名作为子类名后缀。

    在这里插入图片描述

    4. IO流体系图-常用的类

    1. InputStream

    InputStream抽象类是所有类字节输入流的超类

    InputStream 常用的子类

    1. FileInputStream:文件输入流
    2. BufferedInputStream:缓冲字节输入流
    3. ObjectInputStream:对象字节输入流

    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述

    package com.qdu.inputstream;
    
    import org.junit.jupiter.api.Test;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    /**
     * @author dell
     * @version 1.0
     */
    public class FileInputStream_ {
    
        public static void main(String[] args) {
    
    
        }
    
        @Test
        public void readFile01() throws IOException {
            String filePath = "e:\\hello.txt";
            int readDate = 0;
            FileInputStream fileInputStream = new FileInputStream(filePath);
            while ((readDate = fileInputStream.read()) != -1) {
                System.out.print((char) readDate);
            }
            fileInputStream.close();
            System.out.println("");
        }
    
        @Test
        public void readFile02() throws IOException {
            String filePath = "e:\\hello.txt";
            byte[] bytes = new byte[8];//最多只能读取8个字节
            int readLen = 0;
            FileInputStream fileInputStream = new FileInputStream(filePath);
            while ((readLen = fileInputStream.read(bytes)) != -1) {
                System.out.println(new String(bytes, 0, readLen));
            }
            fileInputStream.close();
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    2. FileOutputStream

    在这里插入图片描述

    使用 FileOutputStream 将数据写入到文件中,如果该文件不存在,则创建该文件

    package com.qdu.outputstream;
    
    import org.junit.jupiter.api.Test;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    /**
     * @author dell
     * @version 1.0
     */
    public class FileOutputStream01 {
    
        public static void main(String[] args) {
    
    
        }
    
        @Test
        public void writeFile() throws IOException {
            String filePath = "e:\\a.txt";
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);//写入内容是覆盖之前的内容
    //        FileOutputStream fileOutputStream = new FileOutputStream(filePath, true);//写入的内容是追加到之前的内容
            fileOutputStream.write('H');//写入一个字节
            String string = "hello,world!";//写入一个字符串
            fileOutputStream.write(string.getBytes());//字符串->字节数组
            String str = "qdu,world!";
            fileOutputStream.write(str.getBytes(), 0, 3);//写入指定长度的字符串
            fileOutputStream.close();
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    3. 使用InputStream和FileOutputStream实现文件拷贝

    1. 创建文件的输入流,将文件读入到程序
    2. 创建文件的输出流,将读取到文件数据,写入到指定的文件
    package com.qdu.copy;
    
    import org.junit.jupiter.api.Test;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    
    /**
     * @author dell
     * @version 1.0
     */
    public class FileCopy {
    
        public static void main(String[] args) {
    
        }
    
        @Test
        public void fileCopy() throws IOException {
    
            String startPath = "e:\\hello.txt";
            String endPath = "e:\\hello1.txt";
            FileInputStream fileInputStream = new FileInputStream(startPath);
            FileOutputStream fileOutputStream = new FileOutputStream(endPath);
            byte[] bytes = new byte[1024];
            int readLen = 0;
            while ((readLen = fileInputStream.read(bytes)) != -1) {
                fileOutputStream.write(bytes, 0, readLen);
            }
            System.out.println("拷贝成功...");
            if (fileInputStream != null) fileInputStream.close();
            if (fileOutputStream != null) fileOutputStream.close();
    
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    拷贝到c盘可能存在访问权限的问题…

    4. FileReader和FileWriter介绍

    FIleReader和FileWriter是字符流,即按照字符来操作io

    在这里插入图片描述

    5. FileReader相关方法

    new FileReader(File/String)
    read:每次读取单个字符,返回该字符,如果到文件末尾返回-1
    read(char[]):批量读取多个字符到数组,返回读取到的字符数,如果到文件末尾返回-1
    new String(char[]):将char[]转换成String
    new String(char[], off, len):将char[]的指定部分转换成String
    
    • 1
    • 2
    • 3
    • 4
    • 5

    6. FileWriter相关方法

    new FileWriter(File/String):覆盖模式,相当于流的指针在首端
    new FileWriter(File/String,true):追加模式,相当于流的指针在尾端
    write(int):写入单个字符
    write(char[]):写入指定数组
    write(char[],off,len):写入指定数组的指定部分
    write(string):写入整个字符串
    write(string,off,len):写入字符串的指定部分
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    FileWriter使用后,必须要关闭(close)或刷新(flush),否则,写入不到指定的文件!

    5. 节点流和处理流

    1. 基本介绍

    1. 结点流可以从一个特定的数据源读写数据,如FileReader、FileWriter
      在这里插入图片描述

    2. 处理流(也叫包装流)是“链接”在已存在的流(节点流或处理流)之上,为程序提供更为强大的读写功能,也更加灵活,如BufferedReader、BufferedWriter
      在这里插入图片描述

    2. 节点流和处理流的区别和联系

    1. 节点流是底层流/低级流,直接跟数据源相连接。
    2. 处理流(包装流)包装结点流,既可以消除不同结点流的实现差异,也可以提供更方便的方法来完成输入输出。
    3. 处理流(也叫包装流)对节点流进行包装,使用了修饰器设计模式,不会直接与数据源相连。

    3. 处理流的功能主要体现在以下两方面

    1. 性能的提高:主要以增加缓冲的方式来提高输入输出的效率
    2. 操作的便捷:处理流可能提高了一系列便捷的方法来一次输入输出大批量的数据,使用更加灵活

    4. 处理流-BufferedReader和BufferedWriter

    BufferedReader和BufferedWriter 属于字符流,是按照字符来读取数据的
    关闭处理流,只需要关闭外层流即可

    使用BufferedReader读取文件

    package com.qdu.reader_;
    
    import org.junit.jupiter.api.Test;
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    
    /**
     * @author dell
     * @version 1.0
     */
    public class Reader_ {
    
        public static void main(String[] args) {
    
        }
    
        @Test
        public void BufferedReader_() throws IOException {
            String filePath = "e:\\hello.txt";
            BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }
            bufferedReader.close();
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    使用BufferedWriter写入文件

    package com.qdu.writer_;
    
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    
    /**
     * @author dell
     * @version 1.0
     */
    public class BufferedWriter_ {
    
        public static void main(String[] args) throws IOException {
    
            String filePath = "e:\\hello1.txt";
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath));
            bufferedWriter.write("hello1");
            bufferedWriter.newLine();//换行
            bufferedWriter.write("hello2");
            bufferedWriter.newLine();
            bufferedWriter.write("hello3");
            bufferedWriter.close();
    
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    使用BufferedReader和BufferedWriter完成文本文件拷贝

    package com.qdu.copy;
    
    import java.io.*;
    
    /**
     * @author dell
     * @version 1.0
     */
    public class BufferCopy {
    
        public static void main(String[] args) throws IOException {
    
            String srcFilePath = "e:\\a.txt";
            String destFilePath = "e:\\a1.txt";
    
            BufferedReader bufferedReader = new BufferedReader(new FileReader(srcFilePath));
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(destFilePath));
    
            String line;
            //读取一行时,不会读入换行符
            while ((line = bufferedReader.readLine()) != null) {
                bufferedWriter.write(line);
                bufferedWriter.newLine();
            }
    
            if (bufferedWriter != null) bufferedWriter.close();
            if (bufferedReader != null) bufferedReader.close();
    
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    5. 处理流-BufferedInputStream和BufferedOutputStream

    BufferedInputStream和BufferedOutputStream属于字节流,实现缓冲的输出输入流

    完成图片、音乐的拷贝

    图片、音乐属于二进制文件因此只用字节流来处理

    6. 对象流-ObjectInputStream和ObjectOutputStream

    能够对基本数据类型或对象进行序列化和反序列化

    序列化和反序列化

    1. 序列化就是在保存数据时,保存数据的值和数据类型
    2. 反序列化就是在恢复数据时,恢复数据的值和数据类型
    3. 需要让某个对象支持序列化机制,则必须让其类是可序列化的,为了让某个类是可序列化的,该类必须实现如下两个接口之一:
      Serializable:标记接口,没有方法
      Externalizable:该接口有方法需要实现
      推荐使用第一个接口

    在这里插入图片描述

    7. 对象流介绍

    提供了对基本类型或对象类型的序列化和反序列化的方法

    • ObjectOutputStream 提供 序列化功能
    • ObjectInputStream 提供 反序列化功能

    使用ObjectOutputStream序列化基本数据类型和一个Dog对象(name,age),并保存到data.txt

    package com.qdu.outputstream;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    
    /**
     * @author dell
     * @version 1.0
     */
    public class ObjectOutStream {
    
        public static void main(String[] args) throws IOException {
    
            String filePath = "e:\\data.txt";
    
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(filePath));
            objectOutputStream.writeInt(100);
            objectOutputStream.writeBoolean(true);
            objectOutputStream.writeChar('a');
            objectOutputStream.writeDouble(9.5);
            objectOutputStream.writeUTF("hello,world!!!");
            objectOutputStream.writeObject(new Dog("老黄", 10));
    
            objectOutputStream.close();
    
        }
    
    }
    
    class Dog implements Serializable {
    
        private String name;
        private int age;
    
        public Dog(String name, int age) {
            this.name = name;
            this.age = age;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    使用ObjectInputStream反序列化恢复数据

    package com.qdu.inputstream;
    
    import java.io.*;
    
    /**
     * @author dell
     * @version 1.0
     */
    public class ObjectPutStream {
    
        public static void main(String[] args) throws IOException, ClassNotFoundException {
    
            String filePath = "e:\\data.txt";
    
            ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(filePath));
            //反序列化的顺序要和保存数据(序列化)的顺序一致
    
            System.out.println(objectInputStream.readInt());
            System.out.println(objectInputStream.readBoolean());
            System.out.println(objectInputStream.readChar());
            System.out.println(objectInputStream.readDouble());
            System.out.println(objectInputStream.readUTF());
            System.out.println(objectInputStream.readObject());
    
            objectInputStream.close();
    
        }
    
    }
    
    class Dog implements Serializable {
    
        private String name;
        private int age;
    
        public Dog(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Dog{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    8. 主要事项和细节说明

    1. 读写顺序要一致
    2. 要求实现序列化或反序列化对象,需要实现Serializable
    3. 序列化的类中建议添加SerialVersionUID,为了提高版本的兼容性
    4. 序列化对象时,默认将里面所有属性都进行序列化,但除了static或transient修饰的成员
    5. 序列化对象时,要求里面属性的类型也要实现序列化接口
    6. 序列化具备可继承性,也就是说如果某个类已经实现了序列化,则它的所有子类也已经默认实现了序列化

    9. 标准输入输出流

    在这里插入图片描述

    在这里插入图片描述

    10. 转换流-InputStreamReader和OutputStreamWriter

    介绍

    1. InputStreamReader:Reader的子类,可以将InputStream(字节流)转换成Reader(字符流)
    2. OutputStreamWriter:Writer的子类,可以将OutputStream(字节流)转换成Writer(字符流)
    3. 当处理纯文本数据时,如果使用字符流效率更高,并且可以有效解决中文问题,所以建议将字节流转换成字符流
    4. 可以在使用时指定编码格式(比如:utf-8、gbk、gb2312,iso8859-1等)

    将字节流FileInoutStream转换成字符流InputStreamReader,对文件进行读取,进而包装成BufferReader类

    package com.qdu.transformation;
    
    import java.io.*;
    
    /**
     * @author dell
     * @version 1.0
     */
    public class InputStreamReader_ {
    
        public static void main(String[] args) throws IOException {
    
            String filePath = "e:\\a.txt";
            InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(filePath), "gbk");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = bufferedReader.readLine();
            System.out.println(str);
            bufferedReader.close();
    
    
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    将字节流FileOutputStream转换成字符流OutputStreamWriter,对文件进行写入

    package com.qdu.transformation;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    
    /**
     * @author dell
     * @version 1.0
     */
    public class OutputStreamWriter_ {
    
        public static void main(String[] args) throws IOException {
    
            String filePath = "e:\\qdu.txt";
            String charSet = "gbk";
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(filePath), charSet);
            outputStreamWriter.write("hi,青大牛皮!!!");
            outputStreamWriter.close();
    
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    6. 打印流-PrintStream和PrintWriter

    打印流只有输出流,没有输入流

    PrintStream

    package com.qdu.printstream;
    
    import java.io.IOException;
    import java.io.PrintStream;
    
    /**
     * @author dell
     * @version 1.0
     */
    public class PrintStream_ {
    
        public static void main(String[] args) throws IOException {
    
            PrintStream printStream = System.out;
            //默认情况下,PrintStream输出数据的位置是标准输出,即显示器
            printStream.println("hello!");
            printStream.write("hi".getBytes());
            printStream.close();
    
            System.setOut(new PrintStream("e:\\f1.txt"));
            System.out.println("hello,qdu!!!");
    
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在这里插入图片描述

    PrintWriter

    package com.qdu.transformation;
    
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    /**
     * @author dell
     * @version 1.0
     */
    public class PrintWriter_ {
    
        public static void main(String[] args) throws IOException {
    
            PrintWriter printWriter = new PrintWriter(System.out);
            printWriter.println("h1,你好!!!");
            printWriter.close();
            PrintWriter printWriter1 = new PrintWriter(new FileWriter("e:\\f2.txt"));
            printWriter1.print("hello,你好呀!!!");
            printWriter1.close();
    
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    在这里插入图片描述

    7. Properties类

    1. 基本介绍

    专门用于读写配置文件的集合类
    配置文件的格式:
    键=值

    注意:键值对不需要有空格,值不需要用引号括起来。默认类型是String

    2. 常见方法

    1. load:加载配置文件的键值对到Properties对象
    2. list:将数据显示到指定设备
    3. getProperty(key):根据键获取值
    4. setProperty(key, value):设置键值对到Properties对象
    5. store:将Properties中的键值对存储到配置文件,在idea中,保存信息到配置文件,如果含有中文,会存储为unicode码

    3. 应用案例

    1. 使用Properties类完成对mysql.properties的读取
    package com.qdu.properties_;
    
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Properties;
    
    /**
     * @author dell
     * @version 1.0
     */
    public class Properties01 {
    
        public static void main(String[] args) throws IOException {
    
            Properties properties = new Properties();
            properties.load(new FileReader("src\\mysql.properties"));
            properties.list(System.out);
            String user = properties.getProperty("user");
            String pwd = properties.getProperty("pwd");
            System.out.println(user);
            System.out.println(pwd);
    
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    在这里插入图片描述

    1. 使用Properties类添加key-val到新文件mysql2.properties中
    package com.qdu.properties_;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Properties;
    
    /**
     * @author dell
     * @version 1.0
     */
    public class Properties02 {
    
        public static void main(String[] args) throws IOException {
    
            Properties properties = new Properties();
            properties.setProperty("charset", "utf-8");
            properties.setProperty("user", "jack");
            properties.setProperty("pwd", "abc123");
            properties.store(new FileOutputStream("src\\mysql2.properties"), null);
    
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    在这里插入图片描述

    1. 使用Properties类完成对mysql2.properties的读取,并修改某个key-val

    如果文件有key,则setProperty就是修改
    如果文件没有key,则setProperty就是创建

  • 相关阅读:
    uoj#751-[UNR #6]神隐【交互】
    LeetCode 2609. 最长平衡子字符串
    leetcode - 2938. Separate Black and White Balls
    WAF绕过-漏洞发现之代理池指纹探针 47
    总结下.NET后端已经熟悉或者使用过了这么多东西,有没你喜欢用的
    计算机毕业设计 SSM+Vue美容院管理系统 美容护理管理系统 美容店系统管理Java Vue MySQL数据库 远程调试 代码讲解
    el-table表格——sortable排序 & 出现小数、%时排序错乱
    git基本用法和操作
    塑料透光率测试可测试塑料部件的透明度和纯度
    Comparator::compare设定排序的升序 降序
  • 原文地址:https://blog.csdn.net/qq_52354698/article/details/126424163