IO流
1|0IO流
java.io.File类把文件和目录路径名的抽象表述形式
java把计算机当中的文件和文件夹(目录)封装了一个File类,我们可以使用File类对文件和文件夹进行操作
创建文件/文件夹
删除文件/文件夹
获取文件/文件夹
判断文件
1|1静态方法
static String |
pathSeparator 与系统相关的路径分隔符字符,为方便起见,表示为字符串。 |
---|---|
static char |
pathSeparatorChar 与系统相关的路径分隔符。 |
static String |
separator 与系统相关的默认名称 - 分隔符字符,以方便的方式表示为字符串。 |
static char |
separatorChar 与系统相关的默认名称分隔符。 |
1|2构造方法
Constructor and Description |
---|
File(File parent, String child) |
File(String pathname) |
File(String parent, String child) |
File(URI uri) |
路径可以是真实的,也可以是不存在的
创建一个File对象,只是把字符串封装成File对象,不考虑路径真实情况
可以是文件结尾, 也可以是文件夹结尾
可以是绝对路径,也可以是相对路径
1|3方法
获取文件/文件夹
判断文件/文件夹是否存在
创建文件/文件夹
- 只有在当前路径下文件不存在的时候,创建一个新的空文件
- 只能创建文件,不能不创建文件夹
- 如果构造方法中路径不存在,那么会抛出IOException异常
只能创建目录,路径错误返回false
删除文件/文件夹
对文件夹进行遍历
如果构造方法中给出的路径不存在或者不是一个目录,那么会抛出空指针异常
2|0递归
方法自己调自己。
分为直接递归、间接递归
- 递归一定要有条件限定,保证递归能停下来,否则会发生栈内存溢出
- 递归次数也不能太多,否则也会发生栈内存溢出
- 构造方法,禁止递归
2|1使用场景
当调用方法的时候,主体不变,每次调用方法的参数不同,就可以使用递归。
3|0字节流
3|1字节输出流构造方法
3|2普通方法
返回值 | 方法 |
---|---|
void |
close() 关闭此文件输出流并释放与此流相关联的任何系统资源。 |
protected void |
finalize() 清理与文件的连接,并确保当没有更多的引用此流时,将调用此文件输出流的 close 方法。 |
FileChannel |
getChannel() 返回与此文件输出流相关联的唯一的FileChannel 对象。 |
FileDescriptor |
getFD() 返回与此流相关联的文件描述符。 |
void |
write(byte[] b) 将 b.length 个字节从指定的字节数组写入此文件输出流。 |
void |
write(byte[] b, int off, int len) 将b数组下标为off开始len长度的字节写入到此文件输出流 |
void |
write(int b) 将指定的字节写入此文件输出流。 |
3|3字节输入流构造方法
Constructor and Description |
---|
FileInputStream(File file) 通过打开与实际文件的连接创建一个 FileInputStream ,该文件由文件系统中的 File 对象 file 命名。 |
FileInputStream(FileDescriptor fdObj) 创建 FileInputStream 通过使用文件描述符 fdObj ,其表示在文件系统中的现有连接到一个实际的文件。 |
FileInputStream(String name) 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name 命名。 |
3|4普通方法
Modifier and Type | Method and Description |
---|---|
int |
available() 返回从此输入流中可以读取(或跳过)的剩余字节数的估计值,而不会被下一次调用此输入流的方法阻塞。 |
void |
close() 关闭此文件输入流并释放与流相关联的任何系统资源。 |
protected void |
finalize() 确保当这个文件输入流的 close 方法没有更多的引用时被调用。 |
FileChannel |
getChannel() 返回与此文件输入流相关联的唯一的FileChannel 对象。 |
FileDescriptor |
getFD() 返回表示与此 FileInputStream 正在使用的文件系统中实际文件的连接的 FileDescriptor 对象。 |
int |
read() 从输入流中读取数据的下一个字节,返回0到255范围内的int字节值 |
int |
read(byte[] b) 从输入流中读取数据的下一个字节,返回0到255范围内的int字节值 |
int |
read(byte[] b, int off, int len) 从该输入流读取最多 len 字节的数据为字节数组。 |
long |
skip(long n) 跳过并从输入流中丢弃 n 字节的数据。 |
3|5读取文件
3|6复制文件
4|0字符流
4|1FileReader构造方法
Constructor and Description |
---|
FileReader(File file) 创建一个新的 FileReader ,给出 File 读取。 |
FileReader(FileDescriptor fd) 创建一个新的 FileReader ,给定 FileDescriptor 读取。 |
FileReader(String fileName) 创建一个新的 FileReader ,给定要读取的文件的名称。 |
单个字符读取
多个字符读取
4|2FileWriter构造方法
Constructor and Description |
---|
FileWriter(File file) 给一个File对象构造一个FileWriter对象。 |
FileWriter(File file, boolean append) 给一个File对象构造一个FileWriter对象。 |
FileWriter(FileDescriptor fd) 构造与文件描述符关联的FileWriter对象。 |
FileWriter(String fileName) 构造一个给定文件名的FileWriter对象。 |
FileWriter(String fileName, boolean append) 构造一个FileWriter对象,给出一个带有布尔值的文件名,表示是否附加写入的数据。 |
4|3字符写入
5|0字节缓冲流
5|1构造方法
Constructor and Description |
---|
BufferedInputStream(InputStream in) 创建一个 BufferedInputStream 并保存其参数,输入流 in ,供以后使用。 |
BufferedInputStream(InputStream in, int size) 创建 BufferedInputStream 具有指定缓冲区大小,并保存其参数,输入流 in ,供以后使用。 |
5|2具体方法
Modifier and Type | Method and Description |
---|---|
int |
available() 返回从该输入流中可以读取(或跳过)的字节数的估计值,而不会被下一次调用此输入流的方法阻塞。 |
void |
close() 关闭此输入流并释放与流相关联的任何系统资源。 |
void |
mark(int readlimit) 见的总承包 mark 的方法 InputStream 。 |
boolean |
markSupported() 测试这个输入流是否支持 mark 和 reset 方法。 |
int |
read() 见 read 法 InputStream 的一般合同。 |
int |
read(byte[] b, int off, int len) 从给定的偏移开始,将字节输入流中的字节读入指定的字节数组。 |
void |
reset() 见 reset 法 InputStream 的一般合同。 |
long |
skip(long n) 见 skip 法 InputStream 的一般合同。 |
5|3使用方法
java.io.BufferedOutputStream extends OutputStream字节缓冲输出流
使用步骤:
- 创建FileOutputStream对象,在构造方法中绑定要输出的目的地
- 创建BufferedOutputStream对象,在构造方法中传递FileOutputStream对象,提高FileOutputStream对象效率
- 使用BufferedOutputStream对象中的方法write(),把数据写入到内部缓冲区中
- 使用BufferedOutputStream对象中的方法flush(),把内部缓冲区中的数据,刷新到文件中
- 释放资源
java.io.BufferedlnputStream extends InputStream字节缓冲输入流
使用步骤:
- 创建FilelnputStream对象,在构造方法中绑定要读取的数据源
- 创建BufferedlnputStream对象,在构造方法中传递FilelnputStream对象,提高FilelnputStream对象效率
- 使用BufferedInputStream对象中的方法read(),读取文件
- 释放资源
6|0字符缓冲流
6|1构造方法
Constructor and Description |
---|
BufferedReader(Reader in) 创建使用默认大小的输入缓冲区的缓冲字符输入流。 |
BufferedReader(Reader in, int sz) 创建使用指定大小的输入缓冲区的缓冲字符输入流。 |
6|2普通方法
Modifier and Type | Method and Description |
---|---|
void |
close() 关闭流并释放与之相关联的任何系统资源。 |
Stream<String> |
lines() 返回一个 Stream ,其元素是从这个 BufferedReader 读取的行。 |
void |
mark(int readAheadLimit) 标记流中的当前位置。 |
boolean |
markSupported() 告诉这个流是否支持mark()操作。 |
int |
read() 读一个字符 |
int |
read(char[] cbuf, int off, int len) 将字符读入数组的一部分。 |
String |
readLine() 读一行文字。 |
boolean |
ready() 告诉这个流是否准备好被读取。 |
void |
reset() 将流重置为最近的标记。 |
long |
skip(long n) 跳过字符 |
6|3使用方法
java.io.BufferedWriter extends Writer字符缓冲输出流
使用步骤:
- 创建FileWriter对象,在构造方法中绑定要输出的目的地
- 创建BufferedWriter对象,在构造方法中传递FileWriter对象,提高FileWriter对象效率
- 使用BufferedWriter对象中的方法writer(),把数据写入到缓冲区中
- 刷新缓冲区中的数据
- 释放资源
java.io.BufferedReader extends Reader字符缓冲输入流
使用步骤:
- 创建FileReader对象,在构造方法中绑定要读取的数据源
- 创建BufferedReader对象,在构造方法中传递FileReader对象,提高FileReader对象效率
- 使用BufferedReader对象中的方法read(),读取文件
- 释放资源
7|0Properties
java.util.Properties集合extends Hashtable<k,v> implements Map<k,v>
持久化的属性集,可保存在流中或者从流中加载
是一个唯一和IO流相结合的集合
使用Properties集合中的方法store(),把集合中的临时数据,持久化的写入到硬盘中存储
使用Properties集合中的方法load(),把硬盘中保存的文件(键值对),读取到集合中使用
7|1使用方法及遍历
8|0转换流
8|1OutputStreamWriter
OutputStreamWriter extends Writer是字符流通向字节流的桥梁
使用步骤
- 创建OutputStreamWriter对象,构造犯法中传递字节输出流和指定的编码表
- 使用OutputStreamWriter对象中的write(),把字符转换为字节存储到缓冲区中
- 使用OutputStreamWriter对象中的方法flush(),把内存缓冲区的字节刷新到文件中
- 释放资源
构造方法
Constructor and Description |
---|
OutputStreamWriter(OutputStream out) 创建一个使用默认字符编码的OutputStreamWriter。 |
OutputStreamWriter(OutputStream out, Charset cs) 创建一个使用给定字符集的OutputStreamWriter。 |
OutputStreamWriter(OutputStream out, CharsetEncoder enc) 创建一个使用给定字符集编码器的OutputStreamWriter。 |
OutputStreamWriter(OutputStream out, String charsetName) 创建一个使用命名字符集的OutputStreamWriter。 |
普通方法
Modifier and Type | Method and Description |
---|---|
void |
close() 关闭流,先刷新。 |
void |
flush() 刷新流。 |
String |
getEncoding() 返回此流使用的字符编码的名称。 |
void |
write(char[] cbuf, int off, int len) 写入字符数组的一部分。 |
void |
write(int c) 写一个字符 |
void |
write(String str, int off, int len) 写一个字符串的一部分。 |
8|2InputStreamReader
InputStreamReader extends Reader是字节流通向字符流的桥梁
使用步骤
- 创建InputStreamReader对象,在构造方法中传递字节输入流和指定的编码表名称
- 使用InputStreamReader对象中的方法read()读取文件
- 释放资源
构造方法
Constructor and Description |
---|
InputStreamReader(InputStream in) 创建一个使用默认字符集的InputStreamReader。 |
InputStreamReader(InputStream in, Charset cs) 创建一个使用给定字符集的InputStreamReader。 |
InputStreamReader(InputStream in, CharsetDecoder dec) 创建一个使用给定字符集解码器的InputStreamReader。 |
InputStreamReader(InputStream in, String charsetName) 创建一个使用命名字符集的InputStreamReader。 |
普通方法
Modifier and Type | Method and Description |
---|---|
void |
close() 关闭流并释放与之相关联的任何系统资源。 |
String |
getEncoding() 返回此流使用的字符编码的名称。 |
int |
read() 读一个字符 |
int |
read(char[] cbuf, int offset, int length) 将字符读入数组的一部分。 |
boolean |
ready() 告诉这个流是否准备好被读取。 |
9|0打印流
java.io.PrintStream特点:
- 只负责数据的输出,不负责数据的读取
- 与其他流不同,永远不会抛出IOException
- 特有的方法print println
9|1构造方法
Constructor and Description |
---|
PrintStream(File file) 使用指定的文件创建一个新的打印流,而不需要自动换行。 |
PrintStream(File file, String csn) 使用指定的文件和字符集创建新的打印流,而不需要自动换行。 |
PrintStream(OutputStream out) 创建一个新的打印流。 |
PrintStream(OutputStream out, boolean autoFlush) 创建一个新的打印流。 |
PrintStream(OutputStream out, boolean autoFlush, String encoding) 创建一个新的打印流。 |
PrintStream(String fileName) 使用指定的文件名创建新的打印流,无需自动换行。 |
PrintStream(String fileName, String csn) 创建一个新的打印流,不需要自动换行,具有指定的文件名和字符集。 |
9|2普通方法
Modifier and Type | Method and Description |
---|---|
PrintStream |
append(char c) 将指定的字符附加到此输出流。 |
PrintStream |
append(CharSequence csq) 将指定的字符序列附加到此输出流。 |
PrintStream |
append(CharSequence csq, int start, int end) 将指定字符序列的子序列附加到此输出流。 |
boolean |
checkError() 刷新流并检查其错误状态。 |
protected void |
clearError() 清除此流的内部错误状态。 |
void |
close() 关闭流。 |
void |
flush() 刷新流。 |
PrintStream |
format(Locale l, String format, Object... args) 使用指定的格式字符串和参数将格式化的字符串写入此输出流。 |
PrintStream |
format(String format, Object... args) 使用指定的格式字符串和参数将格式化的字符串写入此输出流。 |
void |
print(boolean b) 打印布尔值。 |
void |
print(char c) 打印一个字符 |
void |
print(char[] s) 打印字符数组。 |
void |
print(double d) 打印双精度浮点数。 |
void |
print(float f) 打印浮点数。 |
void |
print(int i) 打印一个整数。 |
void |
print(long l) 打印一个长整数。 |
void |
print(Object obj) 打印一个对象。 |
void |
print(String s) 打印字符串。 |
PrintStream |
printf(Locale l, String format, Object... args) 使用指定的格式字符串和参数将格式化的字符串写入此输出流的便利方法。 |
PrintStream |
printf(String format, Object... args) 使用指定的格式字符串和参数将格式化的字符串写入此输出流的便利方法。 |
void |
println() 通过写入行分隔符字符串来终止当前行。 |
void |
println(boolean x) 打印一个布尔值,然后终止该行。 |
void |
println(char x) 打印一个字符,然后终止该行。 |
void |
println(char[] x) 打印一个字符数组,然后终止该行。 |
void |
println(double x) 打印一次,然后终止行。 |
void |
println(float x) 打印一个浮点数,然后终止该行。 |
void |
println(int x) 打印一个整数,然后终止行。 |
void |
println(long x) 打印很长时间,然后终止行。 |
void |
println(Object x) 打印一个对象,然后终止该行。 |
void |
println(String x) 打印一个字符串,然后终止行。 |
protected void |
setError() 将流的错误状态设置为 true 。 |
void |
write(byte[] buf, int off, int len) 从指定的字节数组写入 len 个字节,从偏移 off 开始到此流。 |
void |
write(int b) 将指定的字节写入此流。 |
10|0序列化和反序列化流
10|1ObjectOutputStream
java.io.ObjectOutputStream extends OutputStream
构造方法
Modifier | Constructor and Description |
---|---|
protected |
ObjectOutputStream() 为完全重新实现ObjectOutputStream的子类提供一种方法,不必分配刚刚被ObjectOutputStream实现使用的私有数据。 |
ObjectOutputStream(OutputStream out) 创建一个写入指定的OutputStream的ObjectOutputStream。 |
普通方法
Modifier and Type | Method and Description |
---|---|
protected void |
annotateClass(类<?> cl) 子类可以实现此方法,以允许类数据存储在流中。 |
protected void |
annotateProxyClass(类<?> cl) 子类可以实现这种方法来存储流中的自定义数据以及动态代理类的描述符。 |
void |
close() 关闭流。 |
void |
defaultWriteObject() 将当前类的非静态和非瞬态字段写入此流。 |
protected void |
drain() 排除ObjectOutputStream中的缓冲数据。 |
protected boolean |
enableReplaceObject(boolean enable) 启用流来替换流中的对象。 |
void |
flush() 刷新流。 |
ObjectOutputStream.PutField |
putFields() 检索用于缓冲要写入流的持久性字段的对象。 |
protected Object |
replaceObject(Object obj) 该方法将允许ObjectOutputStream的可信子类在序列化期间将一个对象替换为另一个对象。 |
void |
reset() 复位将忽略已写入流的任何对象的状态。 |
void |
useProtocolVersion(int version) 指定在编写流时使用的流协议版本。 |
void |
write(byte[] buf) 写入一个字节数组。 |
void |
write(byte[] buf, int off, int len) 写入一个子字节数组。 |
void |
write(int val) 写一个字节。 |
void |
writeBoolean(boolean val) 写一个布尔值。 |
void |
writeByte(int val) 写入一个8位字节。 |
void |
writeBytes(String str) 写一个字符串作为字节序列。 |
void |
writeChar(int val) 写一个16位的字符。 |
void |
writeChars(String str) 写一个字符串作为一系列的字符。 |
protected void |
writeClassDescriptor(ObjectStreamClass desc) 将指定的类描述符写入ObjectOutputStream。 |
void |
writeDouble(double val) 写一个64位的双倍。 |
void |
writeFields() 将缓冲的字段写入流。 |
void |
writeFloat(float val) 写一个32位浮点数。 |
void |
writeInt(int val) 写一个32位int。 |
void |
writeLong(long val) 写一个64位长 |
void |
writeObject(Object obj) 将指定的对象写入ObjectOutputStream。 |
protected void |
writeObjectOverride(Object obj) 子类使用的方法来覆盖默认的writeObject方法。 |
void |
writeShort(int val) 写一个16位短。 |
protected void |
writeStreamHeader() 提供了writeStreamHeader方法,因此子类可以在流中附加或预先添加自己的头。 |
void |
writeUnshared(Object obj) 将“非共享”对象写入ObjectOutputStream。 |
void |
writeUTF(String str) 此字符串的原始数据写入格式为 modified UTF-8 。 |
10|2ObjectInputStream
java.io.ObjectInputStream extends inputStream
普通方法
Modifier and Type | Method and Description |
---|---|
int |
available() 返回可以读取而不阻塞的字节数。 |
void |
close() 关闭输入流。 |
int |
read() 读取一个字节的数据。 |
int |
read(byte[] b) 读入一个字节数组。 |
int |
read(byte[] b, int off, int len) 读入一个字节数组。 |
Object |
readObject() 读取并返回一个对象。 |
long |
skip(long n) 跳过n个字节的输入。 |
10|3注意事项
- 类必须实现Serializable接口
- 必须存在类对应的class文件
11|0字符集
11|1ascii字符集
11|2GBK字符集
11|3Unicode字符集
__EOF__
作 者:YXH
出 处:https://www.cnblogs.com/YxinHaaa/p/17366487.html
关于博主:编程路上的小学生,热爱技术,喜欢专研。评论和私信会在第一时间回复。或者直接私信我。
版权声明:署名 - 非商业性使用 - 禁止演绎,协议普通文本 | 协议法律文本。
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」