Java-IO
2020-12-13 05:38
标签:类型 nts 缓存 pip start buffered 下标 字符流 div Java 中 的 IO 操作就是对字型节或字符型数据的读写。 InputStream(输入字符流) OutputStream(输出字符流) Reader(输入字节流) Writer(输出字节流) 基于文件(FileInputStream/FileReader ,FileOutputStream/FileWriter) 缓存流(BufferedInputStream/BufferedReader,BufferedOutputStream/BufferedWriter)。支持 mark 转换流(InputStreamReader/OutputStreamWriter)。将字节流转为字符流 对象流(ObjectInputStream/ObjectOutputStream)。用于序列化 https://www.cnblogs.com/jhxxb/p/10523452.html 表中 粗体 字所标出的类代表节点流,必须直接与指定的物理节点关联。斜体字加下划线 标出的类代表抽象基类,无法直接创建实例。 https://blog.csdn.net/nightcurtis/article/details/51324105 https://zhuanlan.zhihu.com/p/25418336 https://snailclimb.gitee.io/javaguide/#/?id=io https://docs.oracle.com/javase/8/docs/technotes/guides/io/index.html Java-IO 标签:类型 nts 缓存 pip start buffered 下标 字符流 div 原文地址:https://www.cnblogs.com/jhxxb/p/11133404.html一、IO 分类
1.按流的方向分类
2.按流的类型分类
二、IO 类使用
1.基类(抽象类:InputStream/Reader,OutputStream/Writer。其它 IO 操作类都是基于这些抽象类)
2.实现类
// 实现了 AutoCloseable 写在 try() 中可自动关闭流
try (FileInputStream fis = new FileInputStream("D:\\tmp\\123.txt")) {
// 字节数组
byte[] b = new byte[1024];
// 用于保存的实际字节数
int hasRead = 0;
while ((hasRead = fis.read(b)) > 0) {
System.out.print(new String(b, 0, hasRead, "UTF-8"));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try (FileReader fr = new FileReader("D:\\tmp\\123.txt")) {
// 字符数组
char[] b = new char[1024];
int hasRead = 0;
while ((hasRead = fr.read(b)) > 0) {
System.out.print(new String(b, 0, hasRead));
}
} catch (IOException e) {
e.printStackTrace();
}
// 复制文件
try (FileInputStream fis = new FileInputStream("D:\\tmp\\123.txt");
FileOutputStream fos = new FileOutputStream("D:\\tmp\\456.txt")) {
byte[] b = new byte[1024];
int hasRead = 0;
while ((hasRead = fis.read(b)) > 0) {
fos.write(b, 0, hasRead);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 复制文件
try (FileReader fr = new FileReader("D:\\tmp\\123.txt");
FileWriter fw = new FileWriter("D:\\tmp\\456.txt")) {
char[] b = new char[1024];
int hasRead = 0;
while ((hasRead = fr.read(b)) > 0) {
fw.write(b, 0, hasRead);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try (FileInputStream fis = new FileInputStream("D:\\tmp\\123.txt");
FileOutputStream fos = new FileOutputStream("D:\\tmp\\456.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos)) {
byte[] b = new byte[1024];
int hasRead = 0;
while ((hasRead = bis.read(b)) > 0) {
bos.write(b, 0, hasRead);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try (FileReader fr = new FileReader("D:\\tmp\\123.txt");
FileWriter fw = new FileWriter("D:\\tmp\\456.txt");
BufferedReader br = new BufferedReader(fr);
BufferedWriter bw = new BufferedWriter(fw)) {
char[] b = new char[1024];
int hasRead = 0;
while ((hasRead = br.read(b)) > 0) {
bw.write(b, 0, hasRead);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try (FileInputStream fis = new FileInputStream("D:\\tmp\\123.txt");
FileOutputStream fos = new FileOutputStream("D:\\tmp\\456.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos))) {
char[] b = new char[1024];
int hasRead = 0;
while ((hasRead = br.read(b)) > 0) {
bw.write(b, 0, hasRead);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
三、IO 分类表
Java 输入/输出流体系中常用的流的分类表
分类
字节输入流
字节输出流
字符输入流
字符输出流
抽象基类
InputStream
OutputStream
Reader
Writer
访问文件
FileInputStream
FileOutputStream
FileReader
FileWriter
访问数组
ByteArrayInputStream
ByteArrayOutputStream
CharArrayReader
CharArrayWriter
访问管道
PipedInputStream
PipedOutputStream
PipedReader
PipedWriter
访问字符串
StringReader
StringWriter
缓冲流
BufferedInputStream
BufferedOutputStream
BufferedReader
BufferedWriter
转换流
InputStreamReader
OutputStreamWriter
对象流
ObjectInputStream
ObjectOutputStream
抽象基类
FilterInputStream
FilterOutputStream
FilterReader
FilterWriter
打印流
PrintStream
PrintWriter
推回输入流
PushbackInputStream
PushbackReader
特殊流
DataInputStream
DataOutputStream
字节流的输入与输出对应图
字符流的输入与输出对应图