当前位置: 首页 > news >正文

IO流(师从韩顺平)

文章目录

  • 文件
    • 什么是文件
    • 文件流
  • 常用的文件操作
    • 创建文件对象相关构造器和方法
      • 应用案例
    • 获取文件的相关信息
      • 应用案例
    • 目录的操作和文件删除
      • 应用案例
  • IO 流原理及流的分类
    • Java IO 流原理
    • IO流的分类
  • IO 流体系图-常用的类
    • IO 流体系图(重要!!!!!)
    • 文件 VS IO流
  • FileInputStream 介绍
    • 简单案例
  • FileOutputStream 介绍
    • 应用实例
  • 文件拷贝
  • 字符流
    • FileReader 和 FileWriter 介绍
    • FileReader 相关方法
    • FileWriter 常用方法
    • 应用案例
  • 节点流和处理流
    • 基本介绍
    • 节点流和处理流一览图
    • 节点流和处理流的区别和联系
    • 处理流的功能主要体现在以下两个方面
    • 处理流-BufferedReader 和 BufferedWriter
    • 处理流-BufferedInputStream 和 BufferedOutputStream
    • 对象流-ObjectInputStream 和 ObjectOutputStream
      • 对象流介绍
      • 对象处理流使用细节
    • 标准输入输出流
    • 转换流-InputStreamReader 和 OutputStreamWriter(解决中文乱码)
    • 打印流-PrintStream 和 PrintWriter
  • Properties 类
    • 看一个需求
      • 传统方法
    • 基本介绍
    • 读文件
    • 修改文件

温馨提示:师从韩顺平

文件

什么是文件

在这里插入图片描述

文件流

在这里插入图片描述

常用的文件操作

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

在这里插入图片描述

应用案例

在这里插入图片描述

package IO;import org.junit.jupiter.api.Test;import java.io.File;
import java.io.IOException;/*** @Author: 韩如意* @CreateTime: 2025-02-26* @Description: no* @Version: 1.0*/public class FileCreate {public static void main(String[] args) {}//方式 1 new File(String pathname)@Testpublic void create01() {String filePath = "C:\\Users\\user\\Desktop\\Java\\File\\news1.txt";File file = new File(filePath);try {file.createNewFile();System.out.println("文件创建成功");} catch (IOException e) {e.printStackTrace();}}//方式 2 new File(File parent,String child) //根据父目录文件+子路径构建@Testpublic void create02() {File parentFile = new File("C:\\Users\\user\\Desktop\\Java\\File");String fileName = "news2.txt";
//这里的 file 对象,在 java 程序中,只是一个对象
//只有执行了 createNewFile 方法,才会真正的,在磁盘创建该文件File file = new File(parentFile, fileName);try {file.createNewFile();System.out.println("创建成功~");} catch (IOException e) {e.printStackTrace();}}//方式 3 new File(String parent,String child) //根据父目录+子路径构建@Testpublic void create03() {String parentPath = "C:\\Users\\user\\Desktop\\Java\\File";String fileName = "news4.txt";File file = new File(parentPath, fileName);try {file.createNewFile();System.out.println("创建成功~");} catch (IOException e) {e.printStackTrace();}}
//下面四个都是抽象类
//
//InputStream
//OutputStream
//Reader //字符输入流
//Writer //字符输出流}

获取文件的相关信息

在这里插入图片描述

应用案例

在这里插入图片描述

package IO;import org.junit.jupiter.api.Test;import java.io.File;/*** @Author: 韩如意* @CreateTime: 2025-02-26* @Description: no* @Version: 1.0*/public class FileInformation {public static void main(String[] args) {}//获取文件的信息@Testpublic void info() {
//先创建文件对象File file = new File("C:\\Users\\user\\Desktop\\Java\\File\\news1.txt");
//调用相应的方法,得到对应信息System.out.println("文件名字=" + file.getName());  //文件名字=news1.txt//getName、getAbsolutePath、getParent、length、exists、isFile、isDirectorySystem.out.println("文件绝对路径=" + file.getAbsolutePath());  //文件绝对路径=C:\Users\\user\Desktop\Java\File\news1.txtSystem.out.println("文件父级目录=" + file.getParent());  //文件父级目录=C:\Users\\user\Desktop\Java\FileSystem.out.println("文件大小(字节)=" + file.length());  //文件大小(字节)=0System.out.println("文件是否存在=" + file.exists());//TSystem.out.println("是不是一个文件=" + file.isFile());//TSystem.out.println("是不是一个目录=" + file.isDirectory());//F}
}

目录的操作和文件删除

在这里插入图片描述

应用案例

在这里插入图片描述

package IO;import org.junit.Test;import java.io.File;/*** @Author: 韩如意* @CreateTime: 2025-02-26* @Description: no* @Version: 1.0*/public class Directory {public static void main(String[] args) {}@Testpublic void m1(){String filePath = "C:\\Users\\user\\Desktop\\Java\\File\\news1.txt";File file = new File(filePath);if(file.exists()){if(file.delete()){System.out.println(filePath+"删除成功");}else {System.out.println("删除失败!");}}else {System.out.println("该文件不存在!");}}//在java编程中目录也被当做文件,只能删除空目录@Testpublic void m2(){String filePath = "C:\\Users\\user\\Desktop\\Java\\File";File file = new File(filePath);if(file.exists()){if(file.delete()){System.out.println(filePath+"删除成功");}else {System.out.println("删除失败!");}}else {System.out.println("该目录不存在!");}}@Testpublic void m3(){String diectoryPath = "C:\\Users\\user\\Desktop\\Java\\File\\file";File file = new File(diectoryPath);if(file.exists()){System.out.println(diectoryPath+"存在...");}else {if(file.mkdir()){System.out.println(diectoryPath+"创建成功");}else {System.out.println(diectoryPath+"创建失败");}}}
}

IO 流原理及流的分类

Java IO 流原理

在这里插入图片描述

IO流的分类

在这里插入图片描述

IO 流体系图-常用的类

IO 流体系图(重要!!!!!)

在这里插入图片描述

文件 VS IO流

文件是存储数据的地方,它在传输数据时,是以流的形式进行传输的
在这里插入图片描述

FileInputStream 介绍

在这里插入图片描述

简单案例

package IO;import org.junit.jupiter.api.Test;import java.io.FileInputStream;
import java.io.IOException;/*** @Author: 韩如意* @CreateTime: 2025-02-27* @Description: no* @Version: 1.0*/public class FileInput {public static void main(String[] args) {}/*** 演示读取文件... * 单个字节的读取,效率比较低* -> 使用 read(byte[] b)*/@Testpublic void readFile01() {String filePath = "C:\\Users\\user\\Desktop\\Java\\File\\news2.txt";int readData = 0;FileInputStream fileInputStream = null;try {
//创建 FileInputStream 对象,用于读取 文件fileInputStream = new FileInputStream(filePath);
//从该输入流读取一个字节的数据。 如果没有输入可用,此方法将阻止。
//如果返回-1 , 表示读取完毕while ((readData = fileInputStream.read()) != -1) {System.out.print((char)readData);//转成 char 显示}} catch (IOException e) {e.printStackTrace();} finally {
//关闭文件流,释放资源.try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}}/*** 使用 read(byte[] b) 读取文件,提高效率*/@Testpublic void readFile02() {String filePath = "C:\\Users\\user\\Desktop\\Java\\File\\news2.txt";
//字节数组byte[] buf = new byte[8]; //一次读取 8 个字节.int readLen = 0;FileInputStream fileInputStream = null;try {
//创建 FileInputStream 对象,用于读取 文件fileInputStream = new FileInputStream(filePath);
//从该输入流读取最多 b.length 字节的数据到字节数组。 此方法将阻塞,直到某些输入可用。
//如果返回-1 , 表示读取完毕
//如果读取正常, 返回实际读取的字节数while ((readLen = fileInputStream.read(buf)) != -1) {System.out.println(new String(buf, 0, readLen));//显示}} catch (IOException e) {e.printStackTrace();} finally {
//关闭文件流,释放资源.try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}
}
}

FileOutputStream 介绍

在这里插入图片描述

应用实例

package IO;import org.junit.jupiter.api.Test;import java.io.FileOutputStream;
import java.io.IOException;/*** @Author: 韩如意* @CreateTime: 2025-02-27* @Description: no* @Version: 1.0*/public class FileOutput {public static void main(String[] args) {}/*** 演示使用 FileOutputStream 将数据写到文件中, * 如果该文件不存在,则创建该文件*/@Testpublic void writeFile() {
//创建 FileOutputStream 对象String filePath = "e:\\a.txt";FileOutputStream fileOutputStream = null;try {
//得到 FileOutputStream 对象 对象
//老师说明
//1. new FileOutputStream(filePath) 创建方式,当写入内容是,会覆盖原来的内容
//2. new FileOutputStream(filePath, true) 创建方式,当写入内容是,是追加到文件后面fileOutputStream = new FileOutputStream(filePath, true);
//写入一个字节
//fileOutputStream.write('H');//
//写入字符串String str = "hsp,world!";
//str.getBytes() 可以把 字符串-> 字节数组
//fileOutputStream.write(str.getBytes());
/*
write(byte[] b, int off, int len) 将 len 字节从位于偏移量 off 的指定字节数组写入此文件输出流
*/fileOutputStream.write(str.getBytes(), 0, 3);} catch (IOException e) {e.printStackTrace();} finally {try {fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}
}

文件拷贝

编程完成图片/音乐 的拷贝

package IO;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;/*** @Author: 韩如意* @CreateTime: 2025-02-27* @Description: no* @Version: 1.0*/public class FileCopy {public static void main(String[] args) {
//完成 文件拷贝,将 e:\\Koala.jpg 拷贝 c:\\
//思路分析
//1. 创建文件的输入流 , 将文件读入到程序
//2. 创建文件的输出流, 将读取到的文件数据,写入到指定的文件. String srcFilePath = "e:\\Koala.jpg";String destFilePath = "e:\\Koala3.jpg";FileInputStream fileInputStream = null;FileOutputStream fileOutputStream = null;try {fileInputStream = new FileInputStream(srcFilePath);fileOutputStream = new FileOutputStream(destFilePath);
//定义一个字节数组,提高读取效果byte[] buf = new byte[1024];int readLen = 0;while ((readLen = fileInputStream.read(buf)) != -1) {
//读取到后,就写入到文件 通过 fileOutputStream
//即,是一边读,一边写fileOutputStream.write(buf, 0, readLen);//一定要使用这个方法}System.out.println("拷贝 ok~");} catch (IOException e) {e.printStackTrace();} finally {try {
//关闭输入流和输出流,释放资源if (fileInputStream != null) {fileInputStream.close();}if (fileOutputStream != null) {fileOutputStream.close();}} catch (IOException e) {e.printStackTrace();}}}
}

字符流

FileReader 和 FileWriter 介绍

在这里插入图片描述

FileReader 相关方法

在这里插入图片描述

FileWriter 常用方法

在这里插入图片描述

应用案例

使用 FileReader 从 story.txt 读取内容,并显示

package IO;import org.junit.jupiter.api.Test;import java.io.FileReader;
import java.io.IOException;/*** @Author: 韩如意* @CreateTime: 2025-02-27* @Description: no* @Version: 1.0*/public class demo01 {public static void main(String[] args) {}/*** 单个字符读取文件*/@Testpublic void readFile01() {String filePath = "C:\\Users\\user\\Desktop\\Java\\File\\story.txt";FileReader fileReader = null;int data = 0;
//1. 创建 FileReader 对象try {fileReader = new FileReader(filePath);
//循环读取 使用 read, 单个字符读取while ((data = fileReader.read()) != -1) {System.out.print((char) data);}} catch (IOException e) {e.printStackTrace();} finally {try {if (fileReader != null) {fileReader.close();}} catch (IOException e) {e.printStackTrace();}}}/*** 字符数组读取文件*/@Testpublic void readFile02() {System.out.println("~~~readFile02 ~~~");String filePath = "C:\\Users\\user\\Desktop\\Java\\File\\story.txt";FileReader fileReader = null;int readLen = 0;char[] buf = new char[8];
//1. 创建 FileReader 对象try {fileReader = new FileReader(filePath);
//循环读取 使用 read(buf), 返回的是实际读取到的字符数
//如果返回-1, 说明到文件结束while ((readLen = fileReader.read(buf)) != -1) {System.out.println(new String(buf, 0, readLen));}} catch (IOException e) {e.printStackTrace();} finally {try {if (fileReader != null) {fileReader.close();}} catch (IOException e) {e.printStackTrace();}}}}

使用 FileWriter 将 “风雨之后,定见彩虹” 写入到 note.txt 文件中

package IO;import java.io.FileWriter;
import java.io.IOException;/*** @Author: 韩如意* @CreateTime: 2025-02-27* @Description: no* @Version: 1.0*/public class deom02 {public static void main(String[] args) {String filePath = "C:\\Users\\user\\Desktop\\Java\\File\\note.txt";
//创建 FileWriter 对象FileWriter fileWriter = null;char[] chars = {'a', 'b', 'c'};try {fileWriter = new FileWriter(filePath);//默认是覆盖写入
// 3) write(int):写入单个字符fileWriter.write('H');
// 4) write(char[]):写入指定数组fileWriter.write(chars);
// 5) write(char[],off,len):写入指定数组的指定部分fileWriter.write("韩顺平教育".toCharArray(), 0, 3);
// 6) write(string):写入整个字符串fileWriter.write(" 你好北京~");fileWriter.write("风雨之后,定见彩虹");// 7) write(string,off,len):写入字符串的指定部分fileWriter.write("上海天津", 0, 2);
//在数据量大的情况下,可以使用循环操作.
} catch (IOException e) {e.printStackTrace();} finally {
//对应 FileWriter , 一定要关闭流,或者 flush 才能真正的把数据写入到文件
//老韩看源码就知道原因.
/*private void writeBytes() throws IOException {this.bb.flip();int var1 = this.bb.limit();int var2 = this.bb.position();assert var2 <= var1;int var3 = var2 <= var1 ? var1 - var2 : 0;if (var3 > 0) {if (this.ch != null) {assert this.ch.write(this.bb) == var3 : var3;} else {this.out.write(this.bb.array(), this.bb.arrayOffset() + var2, var3);}}this.bb.clear();}
*/try {
//fileWriter.flush();
//关闭文件流,等价 flush() + 关闭fileWriter.close();} catch (IOException e) {e.printStackTrace();}}System.out.println("程序结束...");}
}

节点流和处理流

基本介绍

在这里插入图片描述

节点流和处理流一览图

上面红色方框的是节点流,下面红色方框的是处理流

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

处理流-BufferedReader 和 BufferedWriter

在这里插入图片描述

package IO;import java.io.BufferedReader;
import java.io.FileReader;/*** @Author: 韩如意* @CreateTime: 2025-02-27* @Description: no* @Version: 1.0*/public class BufferReader_01 {public static void main(String[] args) throws Exception {String filePath = "e:\\a.java";
//创建 bufferedReaderBufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
//读取String line; //按行读取, 效率高
//说明
//1. bufferedReader.readLine() 是按行读取文件
//2. 当返回 null 时,表示文件读取完毕while ((line = bufferedReader.readLine()) != null) {System.out.println(line);}
//关闭流, 这里注意,只需要关闭 BufferedReader ,因为底层会自动的去关闭 节点流
//FileReader。
/*
public void close() throws IOException {
synchronized (lock) {
if (in == null)
return;
try {
in.close();//in 就是我们传入的 new FileReader(filePath), 关闭了. } finally {
in = null;
cb = null;
}
}
}
*/bufferedReader.close();}
}

在这里插入图片描述


```java
package IO流;import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;/*** @Author: 韩如意* @CreateTime: 2025-02-27* @Description: no* @Version: 1.0*/public class BufferWriter_01 {public static void main(String[] args) throws IOException {String filePath = "e:\\ok.txt";
//创建 BufferedWriter
//说明:
//1. new FileWriter(filePath, true) 表示以追加的方式写入
//2. new FileWriter(filePath) , 表示以覆盖的方式写入BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath));bufferedWriter.write("hello, 韩顺平教育!");bufferedWriter.newLine();//插入一个和系统相关的换行bufferedWriter.write("hello2, 韩顺平教育!");bufferedWriter.newLine();bufferedWriter.write("hello3, 韩顺平教育!");bufferedWriter.newLine();
//说明:关闭外层流即可 , 传入的 new FileWriter(filePath) ,会在底层关闭bufferedWriter.close();}
}

处理流-BufferedInputStream 和 BufferedOutputStream

在这里插入图片描述

package IO;import java.io.*;/*** @Author: 韩如意* @CreateTime: 2025-02-27* @Description: no* @Version: 1.0*/public class Copy {public static void main(String[] args) {
// String srcFilePath = "e:\\Koala.jpg";
// String destFilePath = "e:\\hsp.jpg";
// String srcFilePath = "e:\\0245_韩顺平零基础学 Java_引出 this.avi";
// String destFilePath = "e:\\hsp.avi";String srcFilePath = "e:\\a.java";String destFilePath = "e:\\a3.java";
//创建 BufferedOutputStream 对象 BufferedInputStream 对象BufferedInputStream bis = null;BufferedOutputStream bos = null;try {
//因为 FileInputStream 是 InputStream 子类bis = new BufferedInputStream(new FileInputStream(srcFilePath));bos = new BufferedOutputStream(new FileOutputStream(destFilePath));
//循环的读取文件,并写入到 destFilePathbyte[] buff = new byte[1024];int readLen = 0;
//当返回 -1 时,就表示文件读取完毕while ((readLen = bis.read(buff)) != -1) {bos.write(buff, 0, readLen);}System.out.println("文件拷贝完毕~~~");} catch (IOException e) {e.printStackTrace();} finally {
//关闭流 , 关闭外层的处理流即可,底层会去关闭节点流try {if(bis != null) {bis.close();}if(bos != null) {bos.close();}} catch (IOException e) {e.printStackTrace();}}}
}

对象流-ObjectInputStream 和 ObjectOutputStream

在这里插入图片描述

对象流介绍

在这里插入图片描述

package IO;import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;/*** @Author: 韩如意* @CreateTime: 2025-02-27* @Description: no* @Version: 1.0*/public class ObjectOutStream_ {public static void main(String[] args) throws Exception {
//序列化后,保存的文件格式,不是存文本,而是按照他的格式来保存String filePath = "e:\\data.dat";ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));
//序列化数据到 e:\data.datoos.writeInt(100);// int -> Integer (实现了 Serializable)oos.writeBoolean(true);// boolean -> Boolean (实现了 Serializable)oos.writeChar('a');// char -> Character (实现了 Serializable)oos.writeDouble(9.5);// double -> Double (实现了 Serializable)oos.writeUTF("韩顺平教育");//String
//保存一个 dog 对象oos.writeObject(new Dog1(10,"大黄"));oos.close();System.out.println("数据保存完毕(序列化形式)");}
}
class Dog1 implements Serializable {private String name;private int age;public Dog1(int age, String name) {this.age = age;this.name = name;}
}

在这里插入图片描述

package IO;import java.io.*;/*** @Author: 韩如意* @CreateTime: 2025-02-27* @Description: no* @Version: 1.0*/public class ObjectInputStream_ {public static void main(String[] args) throws Exception {
//序列化后,保存的文件格式,不是存文本,而是按照他的格式来保存String filePath = "e:\\data.dat";// 1.创建流对象ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src\\data.dat"));
// 2.读取, 注意顺序System.out.println(ois.readInt());System.out.println(ois.readBoolean());System.out.println(ois.readChar());System.out.println(ois.readDouble());System.out.println(ois.readUTF());System.out.println(ois.readObject());System.out.println(ois.readObject());  //这个就是 Object dogSystem.out.println(ois.readObject());//  小细节,如果要调用 Dog 方法,需要向下转型,或者啥的,只要代码中出现了这个类或者对象,就要把整个Dog移到可以引用的位置// 3.关闭ois.close();System.out.println("以反序列化的方式读取(恢复)ok~");}
}

对象处理流使用细节

在这里插入图片描述

第五条解读:里面的属性,如果类型是一个自定义的类的话,也要求他实现序列化接口Serializable

标准输入输出流

在这里插入图片描述

System.in 			编译类型: InputStream    运行类型:BufferedInputStream
System.out 			编译类型: PrintStream    运行类型:PrintStream

转换流-InputStreamReader 和 OutputStreamWriter(解决中文乱码)

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

package IO;import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;/*** @Author: 韩如意* @CreateTime: 2025-02-27* @Description: no* @Version: 1.0*/public class InputStreamReader_ {public static void main(String[] args) throws IOException {String filePath = "e:\\a.txt";
//解读
//1. 把 FileInputStream 转成 InputStreamReader
//2. 指定编码 gbk
//InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), "gbk");
//3. 把 InputStreamReader 传入 BufferedReader
//BufferedReader br = new BufferedReader(isr);
//将 2 和 3 合在一起BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "gbk"));
//4. 读取String s = br.readLine();System.out.println("读取内容=" + s);
//5. 关闭外层流br.close();}
}

在这里插入图片描述

/ 1.创建流对象
OutputStreamWriter osw =
new OutputStreamWriter(new FileOutputStream("d:\\a.txt"), "gbk");
// 2.写入
osw.write("hello,韩顺平教育~");
// 3.关闭
osw.close();
System.out.println("保存成功~");

打印流-PrintStream 和 PrintWriter

在这里插入图片描述

PrintStream 案例

package IO;import java.io.IOException;
import java.io.PrintStream; /*** @Author: 韩如意* @CreateTime: 2025-02-27* @Description: no* @Version: 1.0*/
public class PrintStream_ {public static void main(String[] args) throws IOException {PrintStream out = System.out;
//在默认情况下,PrintStream 输出数据的位置是 标准输出,即显示器
/*
源码:
public void print(String s) {
if (s == null) {
s = "null";
}
write(s);
}
*/out.print("john, hello");
//因为 print 底层使用的是 write , 所以我们可以直接调用 write 进行打印/输出out.write("韩顺平,你好".getBytes());out.close();
//我们可以去修改打印流输出的位置/设备
//1. 输出修改成到 "e:\\f1.txt"
//2. "hello, 韩顺平教育~" 就会输出到 e:\f1.txt
//3. public static void setOut(PrintStream out) {
// checkIO();
// setOut0(out); // native 方法,修改了 out
// }System.setOut(new PrintStream("e:\\f1.txt"));System.out.println("hello, 韩顺平教育~");}
}

PrintWriter 案例

package IO;import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;/*** @Author: 韩如意* @CreateTime: 2025-02-27* @Description: no* @Version: 1.0*/public class PrintWriter_ {public static void main(String[] args) throws IOException {
//PrintWriter printWriter = new PrintWriter(System.out);PrintWriter printWriter = new PrintWriter(new FileWriter("e:\\f2.txt"));printWriter.print("hi, 北京你好~~~~");printWriter.close();//flush + 关闭流, 才会将数据写入到文件..}
}

Properties 类

看一个需求

在这里插入图片描述

传统方法

package IO;import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;/*** @Author: 韩如意* @CreateTime: 2025-02-27* @Description: no* @Version: 1.0*/public class Properties01 {public static void main(String[] args) throws IOException {
//读取 mysql.properties 文件,并得到 ip, user 和 pwdBufferedReader br = new BufferedReader(new FileReader("src\\mysql.properties"));String line = "";while ((line = br.readLine()) != null) { //循环读取String[] split = line.split("=");
//如果我们要求指定的 ip 值if ("ip".equals(split[0])) {System.out.println(split[0] + "值是: " + split[1]);}}br.close();}
}

基本介绍

在这里插入图片描述

读文件

package IO;import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;/*** @Author: 韩如意* @CreateTime: 2025-02-27* @Description: no* @Version: 1.0*/public class Properties02 {public static void main(String[] args) throws IOException {
//使用 Properties 类来读取 mysql.properties 文件
//1. 创建 Properties 对象Properties properties = new Properties();
//2. 加载指定配置文件properties.load(new FileReader("src\\mysql.properties"));
//3. 把 k-v 显示控制台properties.list(System.out);
//4. 根据 key 获取对应的值String user = properties.getProperty("user");String pwd = properties.getProperty("pwd");System.out.println("用户名=" + user);System.out.println("密码是=" + pwd);}
}

修改文件

package IO;import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;/*** @Author: 韩如意* @CreateTime: 2025-02-27* @Description: no* @Version: 1.0*/public class Properties03 {public static void main(String[] args) throws IOException {
//使用 Properties 类来创建 配置文件, 修改配置文件内容Properties properties = new Properties();
//创建
//1.如果该文件没有 key 就是创建
//2.如果该文件有 key ,就是修改
/*
Properties 父类是 Hashtable , 底层就是 Hashtable 核心方法
public synchronized V put(K key, V value) {
// Make sure the value is not null
if (value == null) {
throw new NullPointerException();
}
// Makes sure the key is not already in the hashtable. Entry<?,?> tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry<K,V> entry = (Entry<K,V>)tab[index];
for(; entry != null ; entry = entry.next) {
if ((entry.hash == hash) && entry.key.equals(key)) {
V old = entry.value;
entry.value = value;//如果 key 存在,就替换
return old;
}
}
addEntry(hash, key, value, index);//如果是新 k, 就 addEntry
return null;
}
*/properties.setProperty("charset", "utf8");properties.setProperty("user", "汤姆");//注意保存时,是中文的 unicode 码值properties.setProperty("pwd", "888888");
//将 k-v 存储文件中即可properties.store(new FileOutputStream("src\\mysql2.properties"), null);System.out.println("保存配置文件成功~");}
}

相关文章:

IO流(师从韩顺平)

文章目录 文件什么是文件文件流 常用的文件操作创建文件对象相关构造器和方法应用案例 获取文件的相关信息应用案例 目录的操作和文件删除应用案例 IO 流原理及流的分类Java IO 流原理IO流的分类 IO 流体系图-常用的类IO 流体系图&#xff08;重要&#xff01;&#xff01;&…...

Ubuntu+deepseek+Dify本地部署

1.deepseek本地部署 在Ollama官网下载 需要魔法下载 curl -fsSL https://ollama.com/install.sh | sh 在官网找到需要下载的deepseek模型版本 复制命令到终端 ollama run deepseek-r1:7b 停止ollama服务 sudo systemctl stop ollama # sudo systemctl stop ollama.servi…...

Java类中的this操作

在Java中,`this` 是一个关键字,用于引用当前对象的实例。它通常在类的方法或构造器中使用,主要有以下几种用途: 1. 区分成员变量和局部变量 当成员变量与局部变量同名时,使用 `this` 可以明确引用当前对象的成员变量。 public class Person { private String name; …...

云创智城YunCharge 新能源二轮、四轮充电解决方案(云快充、万马爱充、中电联、OCPP1.6J等多个私有单车、汽车充电协议)之新能源充电行业系统说明书

云创智城YunCharge 新能源充电行业系统说明书 ⚡官方文档 ⚡官网地址 1. 引言 随着全球环境保护和能源危机的加剧&#xff0c;新能源汽车行业得到了快速发展&#xff0c;充电基础设施建设也随之蓬勃发展。新能源充电行业系统旨在提供高效、便捷的充电服务&#xff0c;满足电…...

利用STM32TIM自制延迟函数实验

一、实验目的 掌握STM32定时器&#xff08;TIM&#xff09;的工作原理及配置方法学习使用HAL库实现微秒级/毫秒级延时函数理解定时器中断服务程序的编写规范 二、实验原理 ​定时器基础&#xff1a; STM32定时器包含向上计数器、向下计数器、中心对齐模式通过预分频器&#x…...

【STM32F103ZET6——库函数】6.PWM

目录 配置PWM输出引脚 使能引脚时钟 配置PWM 使能PWM 配置定时器 使能定时器时钟 使能定时器 例程 例程说明 main.h main.c PWM.h PWM.c led.h led.c DSQ.h DSQ.c 配置PWM输出引脚 PWM的输出引脚必须配置为复用功能。 注意&#xff1a;需要使用哪个引脚&…...

RabbitMQ系列(四)基本概念之Exchange

在 RabbitMQ 中&#xff0c;Exchange&#xff08;交换机&#xff09; 是消息路由的核心组件&#xff0c;负责根据规则将生产者发送的消息分发到对应的队列&#xff08;Queue&#xff09;中。以下是其核心功能与分类的详细说明&#xff1a; 一、Exchange 的核心作用 消息路由枢…...

解决“ReadTimeoutError:HTTPSConnectionPool”pip安装超时问题

安装pytorch时&#xff0c;出现如下报错信息&#xff1a; pip._vendor.urllib3.exceptions.ReadTimeoutError:HTTPSConnectionPool(host‘files.pythonhosted.org’, port443): Read timed out. 这是由于网络等各种原因导致安装超时引发的&#xff0c;可以按如下方式手动设置延…...

SAP中的屏幕PBO和PAI事件

PBO中的O,OUT,输出&#xff0c;和屏幕显示有关&#xff0c;比如屏幕元素的隐藏与显示&#xff0c;屏幕元素的输入状态的控制。 例如&#xff0c;控制屏幕所有元素为只读模式 LOOP AT SCREEN.SCREEN-INPUT 0.MODIFY SCREEN. ENDLOOP.PAI中的I,IN,输入&#xff0c;和屏幕输入有…...

Linux 环境“从零”部署 MongoDB 6.0:mongosh 安装与数据操作全攻略

前提 完成linux平台部署MongoDB【部署教程】且完成mongosh的安装 由于本人使用的是6.0版本的MongoDB&#xff0c;新版本 MongoDB&#xff08;尤其是 6.0 及以上版本&#xff09;已经不再默认捆绑传统的 mongo shell&#xff0c;而改用新的 MongoDB Shell&#xff08;mongosh&am…...

Ubuntu 下 nginx-1.24.0 源码分析 - ngx_init_cycle 函数 - 详解(4)

详解&#xff08;4&#xff09; 初始化配置转储结构&#xff08;config_dump&#xff09; if (ngx_array_init(&cycle->config_dump, pool, 1, sizeof(ngx_conf_dump_t))! NGX_OK){ngx_destroy_pool(pool);return NULL;}ngx_rbtree_init(&cycle->config_dump_rb…...

Eclipse 编译项目指南

Eclipse 编译项目指南 引言 Eclipse 是一款功能强大的集成开发环境&#xff08;IDE&#xff09;&#xff0c;广泛用于Java、C/C、Python等多种编程语言的开发。在Eclipse中编译项目是进行软件开发的基础步骤。本文将详细介绍如何在Eclipse中编译项目&#xff0c;包括项目设置…...

实现 Leaflet 多类型点位标记与聚合功能的实战经验分享

在现代的地理信息系统&#xff08;GIS&#xff09;应用中&#xff0c;地图功能是不可或缺的一部分。无论是展示商业网点、旅游景点还是公共服务设施&#xff0c;地图都能以直观的方式呈现数据。然而&#xff0c;当数据量较大时&#xff0c;地图上可能会出现大量的标记点&#x…...

C高级——shell(3)

一、shell的选择结构 1.回顾&#xff1a;C语言的选择结构:if , if else if ,if else,switch &#xff08;switch的执行速度最快&#xff09; 2.shell的选择结构&#xff1a; 单分支if 双分支 if else 多分支if elif case..in 1.1 shell的选择结构的格式 --------C语言的格式--…...

Qt 中,**信号与槽(Signals Slots)机制

在 Qt 中&#xff0c;信号与槽&#xff08;Signals & Slots&#xff09;机制 是实现对象间通信的核心模式&#xff0c;通常也被视为一种高效的“通知者模式”。它允许对象在特定事件发生时通知其他对象&#xff0c;且完全解耦。 核心概念 信号&#xff08;Signal&#xff0…...

字符串复制函数strcpy()的使用仿写strcpy()——C语言

strcpy 函数是C语言标准库中的字符串处理函数&#xff0c;用于将一个字符串复制到另一个字符串中。 char *strcpy(char *dest, const char *src); dest 是目标字符串的指针&#xff0c;用于存储复制后的字符串。 src 是源字符串的指针&#xff0c;指向要被复制的字符串。 st…...

想知道两轮差速方形底盘 URDF 咋做,ROS2 配 Rviz 咋显示吗?看这里!

视频讲解 想知道两轮差速方形底盘 URDF 咋做&#xff0c;ROS2 配 Rviz 咋显示吗&#xff1f;看这里&#xff01; 模型概述 一个方形底盘和两个差速驱动轮 URDF 代码 <?xml version"1.0" encoding"utf-8"?> <robot name"diff"> …...

什么是Ollama?什么是GGUF?二者之间有什么关系?

一、Ollama:本地化大模型运行框架 Ollama 是一款开源工具,专注于在本地环境中快速部署和运行大型语言模型(LLM)。它通过极简的命令行操作简化了模型管理流程,支持离线运行、多模型并行、私有化部署等场景。 核心特性 本地化运行:无需依赖云端API,用户可在个人电脑或服务…...

AI触手可及 | 基于函数计算玩转AI大模型

AI触手可及 | 基于函数计算玩转AI大模型 基于函数计算部署AI大模型的优势方案架构图像生成 - Stable Diffusion WebUI部署操作 释放资源部署总结体验反馈 在生成式AI技术加速迭代的浪潮下&#xff0c;百亿级参数的行业大模型正推动产业智能化范式转移。面对数字化转型竞赛&…...

VScode在Windows11中配置MSVC

因为MSVC编译器在vs当中&#xff0c;所以我们首先要安装vs的一部分组件。如果只是需要MSVC的话&#xff0c;工作负荷一个都不需要勾选&#xff0c;在单个组件里面搜索MSVC和windows11 SDK&#xff0c;其中一个是编译器&#xff0c;一个是头文件然后右下角安装即可。搜索Develop…...

基于SSM实现的bbs论坛系统功能实现四

一、前言介绍&#xff1a; 1.1 项目摘要 随着互联网技术的不断进步和普及&#xff0c;网络社区已成为人们获取信息、交流意见、分享经验的重要场所。BBS&#xff08;Bulletin Board System&#xff0c;电子公告板系统&#xff09;论坛系统作为网络社区的一种重要形式&#xf…...

Linux mount命令

Linux mount命令是经常会使用到的命令&#xff0c;它用于挂载Linux系统外的文件。 一、挂载功能介绍 挂载方法&#xff1a;mount DECE MOUNT_POINT 命令使用格式&#xff1a;mount [-fnrsvw] [-t vfstype] [-o options] device dir device&#xff1a;指明要挂载的设备&…...

【RAG生成】生成模块核心技术解密:从理论到实践的全链路优化

RAG知识系列文章 【RAG实践】手把手Python实现搭建本地知识问答系统【RAG进阶】从基础到模块化&#xff1a;深度解析RAG技术演进如何重塑AI知识边界【RAG检索】RAG技术揭秘&#xff1a;检索≠召回&#xff1f;【RAG增强】解密RAG系统排序优化&#xff1a;从基础原理到生产实践…...

JavaWeb-ServletContext应用域接口

文章目录 ServletContext接口简介获取一个ServletContext对象ServletContext接口中的相关方法获取应用域配置参数关于应用域参数的配置要求getContextPath获取项目路径getRealPath获取真实路径log系列方法添加相关日志增删查应用域属性 ServletContext接口简介 ServletContext…...

Vue3状态管理新选择:Pinia使用完全指南

一、为什么需要状态管理&#xff1f; 在Vue应用开发中&#xff0c;当我们的组件树变得复杂时&#xff0c;组件间的数据传递会成为棘手的问题。传统方案&#xff08;如props/$emit&#xff09;在多层嵌套组件中会变得笨拙&#xff0c;这时状态管理工具应运而生。Vue3带来了全新…...

权重生成图像

简介 前面提到的许多生成模型都有保存了生成器的权重,本章主要介绍如何使用训练好的权重文件通过生成器生成图像。 但是如何使用权重生成图像呢? 一、参数配置 ima_size 为图像尺寸,这个需要跟你模型训练的时候resize的时候一样。 latent_dim为噪声维度,一般的设置都是…...

【ESP32S3接入讯飞在线语音识别】

视频地址: 【ESP32S3接入讯飞在线语音识别】 1. 前言 使用Seeed XIAO ESP32S3 Sense开发板接入讯飞实现在线语音识别。自带麦克风模块用做语音输入,通过串口发送字符“1”来控制数据的采集和上传。 语音识别对比 平台api教程评分百度...

RISCV指令集解析

参考视频&#xff1a;《RISC-V入门&进阶教程》1-4-RV32I基本指令集&#xff08;1&#xff09;_哔哩哔哩_bilibili privilege是特权指令集&#xff0c;有点系统调用的感觉&#xff0c;要走内核态。unprivilege指令集有点像普通的函数调用。...

详解 c++ 中的 namespage

C 中的命名空间很特别&#xff0c;其他编程语言基本都没有。命名空间介于函数与类之间&#xff0c;兼顾了二者的一些优点。这篇博客根据 chatgpt 的回答整理。 文章目录 **1. 什么是 namespace&#xff08;命名空间&#xff09;&#xff1f;****2. 语法****3. 使用 namespace 访…...

Qt监控系统远程回放/录像文件远程下载/录像文件打上水印/批量多线程极速下载

一、前言说明 在做这个功能的时候&#xff0c;着实费了点心思&#xff0c;好在之前做ffmpeg加密解密的时候&#xff0c;已经打通了极速加密保存文件&#xff0c;主要就是之前的类中新增了进度提示信号&#xff0c;比如当前已经处理到哪个position位置&#xff0c;发个信号出来…...

Idea 和 Pycharm 快捷键

一、快捷键 二、Pycharm 中怎么切换分支 参考如下 如果在界面右下角 没有看到当前所在的分支&#xff0c;如 “Git:master” 3. 有了 4....

在 Mac mini M2 上本地部署 DeepSeek-R1:14B:使用 Ollama 和 Chatbox 的完整指南

随着人工智能技术的飞速发展&#xff0c;本地部署大型语言模型&#xff08;LLM&#xff09;已成为许多技术爱好者的热门选择。本地部署不仅能够保护隐私&#xff0c;还能提供更灵活的使用体验。本文将详细介绍如何在 Mac mini M2&#xff08;24GB 内存&#xff09;上部署 DeepS…...

2024年国赛高教杯数学建模A题板凳龙闹元宵解题全过程文档及程序

2024年国赛高教杯数学建模 A题 板凳龙闹元宵 原题再现 “板凳龙”&#xff0c;又称“盘龙”&#xff0c;是浙闽地区的传统地方民俗文化活动。人们将少则几十条&#xff0c;多则上百条的板凳首尾相连&#xff0c;形成蜿蜒曲折的板凳龙。盘龙时&#xff0c;龙头在前领头&#x…...

【R包】pathlinkR转录组数据分析和可视化利器

介绍 通常情况下&#xff0c;基因表达研究如微阵列和RNA-Seq会产生数百到数千个差异表达基因&#xff08;deg&#xff09;。理解如此庞大的数据集的生物学意义变得非常困难&#xff0c;尤其是在分析多个条件和比较的情况下。该软件包利用途径富集和蛋白-蛋白相互作用网络&…...

基于大模型的脑出血全周期预测与诊疗方案研究报告

目录 一、引言 1.1 研究背景与意义 1.2 研究目的与创新点 1.3 研究方法与数据来源 二、大模型预测脑出血的原理与技术基础 2.1 大模型概述 2.2 脑出血相关数据收集与预处理 2.3 机器学习算法在预测模型中的应用 2.4 模型训练与优化 三、术前风险预测与准备 3.1 术前…...

高数1.1 函数

1. 定义 1.1 函数 1.2 反函数 1.2.1 求反函数例题 1.3 基本初等函数和初等函数 1.4 基本函数性质...

Java常见设计模式(中):结构型模式

&#x1f308; 引言&#xff1a;设计模式就像乐高积木 适配器&#xff1a;让不同形状的积木完美拼接装饰器&#xff1a;给积木添加炫酷灯光效果代理&#xff1a;遥控积木完成复杂动作组合&#xff1a;将小积木搭建成宏伟城堡 结构型模式 主要用于描述对象之间的关系&#xff…...

【Python修仙编程】(二) Python3灵源初探(3)

第一部分&#xff1a;乾坤袋的秘密与修炼之路 在修仙界&#xff0c;有一个古老的传承&#xff0c;名为《Python无极心法》&#xff0c;它蕴含着强大的力量&#xff0c;能够助修仙者突破重重境界&#xff0c;领悟宇宙天地的奥秘。而要修炼此心法&#xff0c;必须先从基础的“乾…...

Mac本地部署Deep Seek R1

Mac本地部署Deep Seek R1 1.安装本地部署大型语言模型的工具 ollama 官网&#xff1a;https://ollama.com/ 2.下载Deepseek R1模型 网址&#xff1a;https://ollama.com/library/deepseek-r1 根据电脑配置&#xff0c;选择模型。 我的电脑&#xff1a;Mac M3 24G内存。 这…...

可以免费无限次下载PPT的网站

前言 最近发现了一个超实用的网站&#xff0c;想分享给大家。 在学习和工作的过程中&#xff0c;想必做PPT是一件让大家都很头疼的一件事。 想下载一些PPT模板减少做PPT的工作量&#xff0c;但网上大多精美的PPT都是需要付费才能下载使用。 即使免费也有次数限制&#xff0…...

c++作业

练习题&#xff1a; #include <iostream> #include <cstring> using namespace std;class mystring {char* p;int len; public:mystring();mystring(const char* p);~mystring();void copy(const mystring& str);void append(const mystring& str);void sh…...

补题蓝桥杯14届JavaB组第4题

算法&#xff1a;动态规划 需要两个一维数组来进行dp 一个用来记录到当前位置的最短时间&#xff0c;另一个用来记录到达当前位置传送门的最短时间 到达传送门的时间需要进行判断&#xff0c;如果上一次传送到达传送门&#xff0c;需要判断上一次传送到这的位置在当前传送门…...

DeepSeek 15天指导手册——从入门到精通 PDF(附下载)

DeepSeek使用教程系列--DeepSeek 15天指导手册——从入门到精通pdf下载&#xff1a; https://pan.baidu.com/s/1PrIo0Xo0h5s6Plcc_smS8w?pwd1234 提取码: 1234 或 https://pan.quark.cn/s/2e8de75027d3 《DeepSeek 15天指导手册——从入门到精通》以系统化学习路径为核心&…...

【北京迅为】itop-3568 开发板openharmony鸿蒙烧写及测试-第1章 体验OpenHarmony—烧写镜像

瑞芯微RK3568芯片是一款定位中高端的通用型SOC&#xff0c;采用22nm制程工艺&#xff0c;搭载一颗四核Cortex-A55处理器和Mali G52 2EE 图形处理器。RK3568 支持4K 解码和 1080P 编码&#xff0c;支持SATA/PCIE/USB3.0 外围接口。RK3568内置独立NPU&#xff0c;可用于轻量级人工…...

C#开发——时间间隔类TimSpan

TimeSpan 是 C# 中的一个结构&#xff08; struct &#xff09;&#xff0c;用于表示时间间隔或持续时间。它位于 System 命名空间中&#xff0c;是处理时间相关操作时非常重要的工具&#xff0c;尤其是在计算两个日期或时间之间的差值、表示时间段或执行时间相关的运算…...

第002文-kali虚拟机安全与网络配置

1、kali系统介绍 kali是一个基于Linux kernel的操作系统&#xff0c;由BackTrack(简称BT)发展而来。BackTrack是2006年推出的一个用于渗透测试及黑客攻防的专用平台&#xff0c;基于Knoppix(linux的一个发行版)开发。BackTrack版本周期&#xff1a;2006年的起始版本BackTrack …...

【HarmonyOS Next】鸿蒙应用折叠屏设备适配方案

【HarmonyOS Next】鸿蒙应用折叠屏设备适配方案 一、前言 目前应用上架华为AGC平台&#xff0c;都会被要求适配折叠屏设备。目前华为系列的折叠屏手机&#xff0c;有华为 Mate系列&#xff08;左右折叠&#xff0c;华为 Mate XT三折叠&#xff09;&#xff0c;华为Pocket 系列…...

关于“你对 Spring Cloud 的理解”

Spring Cloud 是一个基于 Spring Boot 的微服务框架&#xff0c;旨在简化分布式系统的开发、部署和管理。它提供了一系列工具和组件&#xff0c;帮助开发者构建和管理微服务架构。以下是对 Spring Cloud 的核心理解&#xff1a; 1. 微服务架构的支持 Spring Cloud 提供了对微…...

微信小程序-二维码绘制

wxml <view bindlongtap"saveQrcode"><!-- 二维码 --><view style"position: absolute;background-color: #FFFAEC;width: 100%;height: 100vh;"><canvas canvas-id"myQrcode" style"width: 200px; height: 200px;ba…...

c++中如何打印未知类型对象的类型

在 C 中要打印未知类型对象的类型名称&#xff0c;可以通过以下方法实现&#xff1a; 目录 方法一&#xff1a;使用 typeid 和 name()&#xff08;需包含 &#xff09; 使用示例&#xff1a; 问题与改进&#xff1a; 方法二&#xff1a;编译时类型名称&#xff08;C17 起&…...