Java入门 - 09 IO流
IO流
前言
本文为B站Java教学视频BV1Kb411W75N的相关笔记,主要用于个人记录与分享,如有错误欢迎留言指出。
本章笔记涵盖视频内容P581~P617
1.File类
- 定义:File类是文件和文件目录路径的抽象表示形式;File能新建,删除,重命名文件和目录,但File不能访问文件内容。如果需要访问文件内容,则需要使用输入/输出流。
1.1 File类的实例化
public void test1(){
//构造器1
File file1 = new File("hello.txt"); //相对路径
File file2 = new File("D:\\workspace_idea\\day08\\he.txt"); //绝对路径
//构造器2
File file3 = new File("D:\\workspace_idea","day08");//父路径下的子路径
//构造器3
File file4 = new File(file3,"hi.txt"); //应用其它File类的路径
}
- 注意事项
- 相对路径:相较于某个路径下,指明的路径
绝对路径:包含盘符在内的文件或文件目录的路径 - 路径中的每级目录之间都用一个路径分割符隔开
windows和DOS系统默认使用""来表示
UNIX和URL使用"/"来表示
为保证平台兼容,java提供了File类的常量public static final String separator,表示动态分隔符 - 想要在Java程序中表示一个真实存在的文件或目录,那么必须有一个File对象,但是Java程序中的一个File对象,具有的不一定是真实存在的文件或目录
- 相对路径:相较于某个路径下,指明的路径
1.2 File类的常用方法
-
File类的获取功能
-
public String getAbsolutePath():获取绝对路径
-
public String getPath():获取路径
-
public String getName():获取名称
-
public String getParent():获取上层文件目录路径。若无,返回null
-
public long length():获取文件长度(字节数),不能获取目录的长度
-
public long lastModified():获取最后一次的修改时间(时间戳)
-
public String[] list:获取指定目录下的所有文件,或者文件目录的名称数组
-
public String[] listFiles:获取指定目录下的所有文件,或者文件目录的File数组
-
File类的重命名功能
-
public boolean renameTo(File dest):把文件重命名为指定的文件路径
- 要想保证返回true,需要让调用File类在硬盘中实际存在,且dest不能在硬盘中存在(被占用)
-
File类的判断功能
-
public boolean isDirectory():判断是否是文件目录
-
public boolean isFile():判断是否是文件
-
public boolean exists():判断是否存在
-
public boolean canRead():判断是否是可读
-
public boolean canWrite():判断是否可写
-
public boolean isHidden():判断是否是隐藏
-
File类的创建与删除功能
-
public boolean createNewFile():创建文件。若文件存在,则不创建,返回false
-
public boolean mkdir():创建文件目录。如果此文件目录已存在,则不创建。如果此文件目录上层目录不存在,也不创建
-
public boolean mkdirs():创建文件目录。如果此文件目录已存在,则不创建。如果此文件目录上层目录不存在,则一并创建
-
public boolean delete():删除文件或者文件夹
2.IO流
2.1 流的分类
-
按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)
-
按数据流的流向不同分为:输入流,输出流
-
按流的角色的不同分为:节点流,处理流
抽象基类 字节流 字符流 输入流 InputStream Reader 输出流 OutputStream Writer -
流的体系结构
抽象基类 节点流(文件流) 缓冲流(处理流的一种) InputStream FileInputStream BufferedInputStream OutputStream FileOutputStream BufferedOutputStream Reader FileReader BufferedReader Writer FileWriter BufferedWriter
2.2 节点流
2.2.1 FileReader/FileWriter
- FileReader基本操作
public class Test{
public static void main(String[] args) {
}
public void testFileReader(){
//1.实例化File类的对象,指明要操作的文件
File file = new File("hello.txt"); //相对路径
try {
//2.提供具体的流
FileReader fr = new FileReader(file);
//3.数据的读入
//read():返回读入的一个字符,如果达到文件末尾,返回-1
int data = 0;
while((data = fr.read()) != -1){
System.out.print((char)data);
}
//4.流的关闭操作
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
异常的处理:为了保证流资源一定可以执行关闭操作,不推荐使用throw,而是需要用try-catch-finally处理
读入的文件一定要存在,否则就会报FileNotFoundException
*/
- 使用read的重载方法优化操作FileReader
public class Test{
public static void main(String[] args) {
}
public void testFileReader1(){
//1.File类的实例化
File file = new File("hello.txt");
try {
//2.FileReader流的实例化
FileReader fr = new FileReader(file);
//3.读入的操作
//read(char[] cbuf):返回每次读入cbuf数组中的字符的个数,如果达到文件末尾,返回-1
char[] cbuf = new char[5]; //每次都read长度为5的字符
int len;
while((len = fr.read(cbuf)) != -1){
//方式一:
//错误的写法
// for(int i = 0;i < cbuf.length;i++){
// System.out.print(cbuf[i]);
// }
//正确的写法
for(int i = 0;i < len;i++){
System.out.print(cbuf[i]);
}
//方式二:
//错误的写法
// String str = new String(cbuf);
// System.out.print(str);
//正确的写法
String str = new String(cbuf,0,len);
System.out.print(str);
}
//4.资源的关闭
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
重载后的read可以一次性读入多个字符,效率得到提升
需要注意数组的覆盖问题
*/
- FileWriter基本操作
public class Test{
public static void main(String[] args) {
}
public void testFileWriter(){
//1.提供File类的对象,指明写出到的文件
File file = new File("hello1.txt");
try {
//2.提供FileWriter的对象,用于数据的写出
FileWriter fw = new FileWriter(file,false);
//3.写出的操作
fw.write("AAA");
fw.write("BBBBBBB");
//4.流资源的关闭
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
写入操作,对应的File可以不存在,不会报异常
File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件
File对应的硬盘中的文件如果存在:
如果流使用的构造器是:FileWriter(file,false)/FileWriter(file):对原有文件的覆盖
如果流使用的构造器是:FileWriter(file,true):不会对原有文件覆盖,而是在原有文件基础上追加内容
*/
2.2.2 FileInputStream/FileOutputStream
public void testFileInputOutputStream(){
File srcFile = new File("1.jpg");
File destFile = new File("2.jpg");
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
byte[] buffer = new byte[5];
int len;
while((len = fis.read(buffer)) != -1){
fos.write(buffer,0,len);
}
fos.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
对于文本文件(txt/java/c/cpp),使用字符流处理
对于非文本文件(jpg/mp3/mp4/doc),使用字节流处理
*/
2.3 处理流
2.3.1 缓冲流
- 定义:缓冲流通过内部提供一个缓冲区的方式,提升了流的读取,写入速度
BufferedInputStream/BufferedOutputStream
public class Test{
public static void main(String[] args) {
//1.造文件
File scrFile = new File("1.jpg");
File destFile = new File("3.jpg");
try {
//2.造流
//2.1 造节点流
FileInputStream fis = new FileInputStream(scrFile);
FileOutputStream fos = new FileOutputStream(destFile);
//2.2 造缓冲流
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
//3.复制的细节:读取,写入
byte[] buffer = new byte[10];
int len;
while((len = bis.read(buffer)) != -1){
bos.write(buffer,0,len);
}
//4.资源关闭
//关闭外层流的同时,内层流也会自动的进行关闭。
bos.close();
bis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedReader/BufferedWriter
public class Test{
public static void main(String[] args) {
try {
//匿名创建处理流
BufferedReader br = new BufferedReader(new FileReader(new File("1.txt")));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("2.txt")));
//读写操作
//方式一:使用char[]数组
// char[] cbuf = new char[1024];
// int len;
// while((len = br.read(cbuf)) != -1){
// bw.write(cbuf,0,len);
// }
//方式二:使用String
String data;
while((data = br.readLine()) != null){
//方法一:
bw.write(data + "\n"); //data中不包含换行符(需要额外添加换行符)
//方法二:
bw.write(data);
bw.newLine(); //相当于每写入一次就新创建一行("\n")
}
//关闭资源
bw.close();
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
flush()是在写入时用于刷新缓冲区的,程序默认缓冲区满了后才会写入内容,flush会强制让缓冲区输出内容
*/
2.3.2 转换流
-
定义:转换流是字符流的一种,其提供字节流与字符流之间的转换
- InputStreamReader:将一个字节的输入流转换为字符的输入流
- OutputStreamWriter:将一个字符的输出流转换为字节的输出流
-
解码:字节,字节数组 → 字符数组,字符串
编码:字符数组,字符串 → 字节,字节数组
public class Test{
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("1.txt");
//使用系统默认的字符集读取输入流
// InputStreamReader isr = new InputStreamReader(fis);
//使用指定的字符集读取输入流
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
char[] cbuf = new char[20];
int len;
while((len = isr.read(cbuf)) != -1){
String str = new String(cbuf,0,len);
System.out.print(str);
}
isr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.3.3 标准输入输出流
-
System.in:标准的输入流,默认从键盘输入,类型是InputStream
System.out:标准的输入流,默认从控制台输出,类型是PrintStream(OutputStream的子类)
-
System类的setIn(InputStream is) / setOut(PrintStream ps)方式重新指定输入和输出的流
//通过转化流与标准输出输入实现Scanner
public class Test{
public static void main(String[] args) {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
while(true){
String data = null;
try {
data = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if("test".equalsIgnoreCase(data)){
System.out.println("结束!");
break;
}
}
}
}
2.3.4 打印流
- 定义:PrintStream和PrintWriter实现将基本数据类型的数据格式转化为字符串输出
- PrintStream和PrintWriter有自动flush功能
- 提供了一系列重载的print()和println()方法,用于多种数据类型的输出
public class Test{
public static void main(String[] args) {
PrintStream ps = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File("D:\\IO\\text.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//创建打印输出流,设置为自动刷新模式(写入换行符的时候会刷新输出缓冲区)
ps = new PrintStream(fos,true);
if(ps != null){
System.setOut(ps);
}
for(int i = 0;i <= 255; i++){
System.out.print((char)i);
}
}
}
/*
这样子print的内容都会输出到指定的文件下,相当于重写了print
*/
2.3.5 数据流
- 定义:DataInputStream 和 DataOutputStream,用于读取或写出基本数据类型的变量或字符串
public class Test{
public static void main(String[] args) {
try {
DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
dos.writeUTF("HARRY");
dos.flush();
dos.writeInt(23);
dos.flush();
dos.writeBoolean(true);
dos.flush();
dos.close();
//读取不同类型的数据的顺序要与当初写入文件时,保存的数据的顺序一致
DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
String name = dis.readUTF();
int age = dis.readInt();
boolean isMale = dis.readBoolean();
System.out.println(name);
System.out.println(age);
System.out.println(isMale);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.对象流
-
定义:ObjectInputStream和ObjectOutputStream用于存储和读取基本数据类型数据或对象的处理流,它可以把Java的对象写入到数据源中,也能把对象从数据源中还原回来
-
序列化:用ObjectOutputStream类保存基本数据类型或对象的机制
反序列化:用ObjectInputStream类读取基本数据类型或对象的机制
-
ObjectInputStream和ObjectOutputStream不能序列化static和transient修饰的成员变量
public class Test{
public static void main(String[] args) {
//序列化:将内存中的java对象保存到磁盘中
//使用ObjectOutputStream实现
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("1.dat"));
oos.writeObject(new String("TestTest"));
oos.flush();
oos.close();
//反序列化:将磁盘文件中的对象还原为内存中的一个java对象
//使用ObjectInputStream实现
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.dat"));
Object obj = ois.readObject();
String str = (String)obj;
System.out.println(str);
ois.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
- 自定义类序列/反序列化
/*
自定义类需要满足以下要求,方可序列化
1.需要实现Serializable接口
2.当前类提供一个全局常量:serialVersionUID
3.除了当前类需要实现Serializable接口之外,还必须保证其内部所有属性都可以序列化
4.ObjectInputStream和ObjectOutputStream不能序列化static和transient修饰的成员变量
*/
class Person implements Serializable{
public static final long serialVersionUID = 64846545648L;
private String name;
private int age;
}
/*
serialVersionUID用来表明类的不同版本,其目的是对序列化对象进行版本控制。
如果类没有显式定义这个静态常量,它的值是自动生成的。若类的实例变量做了修改,serialVersionUID可能发生变化(无法反序列化)
*/
4. 随机存取文件流RandomAccessFile
-
定义:RandomAccessFile这个类实现了DataInput和DataOutput两个接口,意味着它既可以作为一个输入流也可以作为一个输出流。它支持"随机访问"的方式,程序可以直接跳到文件的任意地方来读,写文件
-
如果RandomAccess作为输出流时,写出到的文件如果不存在,则在执行过程中自动创建。如果写出到的文件存在,则会对原有文件内容进行覆盖(默认情况下,从头覆盖)
-
有些类似C风格的读写方式
public class Test{
public static void main(String[] args) {
try {
RandomAccessFile raf1 = new RandomAccessFile(new File("1.jpg"),"r");
RandomAccessFile raf2 = new RandomAccessFile(new File("2.jpg"),"rw");
byte[] buffer = new byte[1024];
int len;
while((len = raf1.read(buffer)) != -1){
raf2.write(buffer,0,len);
}
raf1.close();
raf2.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 使用RandomAccessFile实现数据的插入效果
public class Test{
public static void main(String[] args) {
try {
RandomAccessFile raf1 = new RandomAccessFile("hello.txt","rw");
raf1.seek(3);//将指针调到角标为3的位置
//保存指针3后面的所有数据到StringBuilder中
StringBuilder builder = new StringBuilder((int)new File("hello.txt").length());
byte[] buffer = new byte[20];
int len;
while((len = raf1.read(buffer)) != -1){
builder.append(new String(buffer,0,len));
}
//调回指针,写入xyz
raf1.seek(3);
raf1.write("xyz".getBytes());
//将StringBuilder中的数据写入到文件中
raf1.write(builder.toString().getBytes());
raf1.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)