最近学习了IO流的相关内容,内容有点多所以打算总结一下便于以后查看。
IO流是用来处理二进制文件或者文本文件的,IO流简单分为输入流和输出流,输入输出流又分为字符输入输出流和字节输入输出流。
第一个就是FileInputStrem和FileOutputStrem。这是它的类图,InputStrem是所有字节输入流的超类,同时它还是个抽象类。
下面是它的常见用法,常用来处理二进制文件。
import java.io.FileInputStream;
import java.io.IOException;
/**
* @author~~ version 2.0
*/
public class test {
public static void main(String[] args) throws IOException {
String path = "e:\\a.txt";
FileInputStream fileInputStream = new FileInputStream(path);
int rend = 0;
fileInputStream.read()返回读入的字节数据,如果读到末尾就返回-1
while ((rend=fileInputStream.read())!=-1){
System.out.println((char)rend);
}
//关闭文件流释放资源
fileInputStream.close();
}
}
//用字节数组读取,增加效率
byte b[] = new byte[8];
int rend1 = 0;
while ((rend1=fileInputStream.read(b))!=-1){
System.out.println(new String(b,0,rend1));
}
下面是FileOutputStrem的类图,OutputStrem是所有字节输出流的父类。用来处理文本文件。
//类似FileInputStrem
import org.junit.jupiter.api.Test;
import java.io.FileOutputStream;
/**
* @author~~ version 2.0
*/
public class output {
public static void main(String[] args) {
String path = "e:\\p.txt";
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(path);
fileOutputStream.write('p');
} catch (Exception e) {
throw new RuntimeException(e);
}
fileOutputStream.close
}
}
//同样可以用字节数组来提高效率
byte b[] = new byte[8];
int reader = 0;
try {
while (((reader = fileInputStream.read(b))!= -1)) {
fileOutputStream.write(b, 0, reader);
}
下面就是字符输入输出流。主要有FileWriter和FileReader。
Writer是所有字符输入流的父类,我们可以看见Writer并不是FileWriter的直接父类,平时我们使用FileWriter更多。因为它更方便简单,还具有自动刷新的功能,可以在每次输出后将数据刷新到磁盘上。
下面是FileWriter的常见用法。
package reader;
import java.io.FileWriter;
import java.io.IOException;
/**
* @author~~ version 2.0
*/
public class read {
public static void main(String[] args) {
FileWriter fileWriter = null;
String path = "e://a.txt";
try {
fileWriter = new FileWriter(path);
//可以写入指定字符数组
//指定字符数组的指定部分
//字符串
//字符串的指定部分
fileWriter.write('p');
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
//注意一定要关闭FileWriter对应的流或者刷新才能完成文件的写入,因为真正的写入文件实在close方法中
完成的。具体的大家可以追一下源码。
fileWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
Reader是所有字符输出流的父类,与Writer类似。
import java.io.*;
public class FileReaderExample {
public static void main(String[] args) {
// 指定读取文件的路径
String filePath = "example.txt";
try {
// 创建 FileReader 对象
FileReader fileReader = new FileReader(filePath);
// 建立一个缓冲区,用来存储读取的字符
char[] buffer = new char[1024];
// 读取字符,每次读取 1024 个字符
int length = 0;
while ((length = fileReader.read(buffer)) != -1) {
// 将字符转换为字符串并输出
System.out.println(new String(buffer, 0, length));
}
// 关闭文件流
fileReader.close();
} catch (IOException e) {
// 文件读取异常处理
e.printStackTrace();
}
}
}
紧接着就是处理流(包装流)
包装流的功能相比节点流来说功能更加完善,拓展的许多功能例如文件加密等。同样包装流也分为字节和字符两种。主要采用了修饰器模式大家可以参考这篇文章。https://mp.csdn.net/mp_blog/creation/editor/131151482
对象处理流:
对象处理流对象处理流实际上是在序列化和反序列化对象时使用的一种机制 。详细内容可以参考这篇文章。
转换流:
从图中我们可以看出,它继承了Reader所以它是一个字符转换流,从它的构造器中能知道,它能接受InputStrem的子类同时我们还可以指定编码方式。
package 字符转换流;
import java.io.*;
/**
* @author~~ version 2.0
*/
public class change {
public static void main(String[] args) throws IOException {
String path = "e://a.txt";
//下面这个也可以分开写
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new
FileInputStream(path), "utf-8"));
System.out.println(bufferedReader.readLine());
bufferedReader.close();
}
}
打印流
打印流只有输出流,没有输入流,有PrintStrem和PrintWriter。
import java.io.*;
public class PrintStreamExample {
public static void main(String[] args) {
String filename = "output.txt";
String text = "Hello, World!";
try (PrintStream ps = new PrintStream(new FileOutputStream(filename))) {
ps.println(text); // 写入文件
ps.println(text); // 写入文件
} catch (IOException e) {
e.printStackTrace();
}
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line); // 打印到控制台
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
最后是Properties
是用于专门用来读取配置文件的存放的也是键值对,值得默认类型是String,不需要用引号。他是Hashtable的子类。
package proptities;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
/**
* @author~~ version 2.0
*/
public class proptites {
public static void main(String[] args) {
Properties properties = new Properties();
try {
//load是加载配置文件的键值对到Properties对象
properties.load(new FileReader("src\\mysquals.proptitise"));
//getProperty(key)获取对应的值
properties.getProperty("id")
} catch (IOException e) {
throw new RuntimeException(e);
}
//list指定输出的设备,可以是文件也可以是控制台
properties.list(System.out);
System.out.println(properties.getProperty("id"));
}
}
package proptities;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
* @author~~ version 2.0
*/
public class p2 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
//设置键值对到Propertites对象
properties.setProperty("id","444");
properties.setProperty("username","pang");
//将键值对存储到配置文件中,第二个参数是设置注释,如果没有就设置为null,如果键值对有中文
//默认按 Unicode码存储,调用load是会自动解码
properties.store(new FileOutputStream("src\\mysqual2"),null);
}
}
以上就是我对常用IO流的简单梳理。