2023/7/24 java基础复习(文件读写)

一、流

1、概念

流是一组有顺序的,有其点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的数据传输。java根据不同的传输特性将流抽象为各种类。

2、流的分类

(1)字符流

本质上是基于字节流读取对象时,去查了指定的码表。根据不同的编码对字符进行高效操作的流对象。
常见的流对象类有FileReader(读),FileWriter(写)。

(2)字节流

当对文件读取到不需要编码或解码的情况时使用字节流。比如图片、视频数据。
常见的字节流可实现类有FileInputStream、FileOutputStream、ObjectInputStream、ObjectOutputStream。

二、字符文件读写(FileReader、FileWriter)

1、单字符读取

FileReader.read()
无参数,返回值为读到的字符的整形格式,没读到字符返回-1。

try{
    FileReader fileReader = new FileReader("io.txt");
    int ch1 = -1;
    while ((ch1 = fileReader.read()) != -1) {
        System.out.println((char)ch1);
    }
}catch (FileNotFoundException e) {
    e.printStackTrace();
}catch (IOException e) {
    e.printStackTrace();
}

2、指定数量字符读写

FileReader.read(ch:char[]):
一个参数,参数类型为字符数组类型;返回值为整形,返回读取字符的个数,个数为零返回-1。每次读取字符的最大数量由数组长度决定。
FileWriter.write(ch:char[],int startId,int length):
三个参数,第一个参数为字符数组,是将要写入的字符内容;第二个参数为整形数据,是写入数据开始的数组下标;第三个参数为整形数据,是写入数据需要写入的字符数量。无返回值。

FileReader fileReader = null;
FileWriter fileWriter = null;
try {
    fileReader = new FileReader("io.txt");
    fileWriter = new FileWriter("back.txt");
    char[] ch = new char[10];
//          字节流读写操作使用byte[]
    int length = -1;
    while ((length = fileReader.read(ch)) != -1) {
        System.out.println(length);
        System.out.println(ch);
        fileWriter.write(ch,0,length);
    }
    System.out.println(length);
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException E) {
    E.printStackTrace();
} finally {
    if(fileWriter != null) {
        try{
             fileWriter.close();
        }catch (IOException E) {
            E.printStackTrace();
        }
    }
    if(fileReader != null) {
        try {
            fileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

三、字节文件读写

fileInputStream.read(buffer):
一个参数,参数类型为字节数组。返回值为整形,返回读取字节的个数,个数为零返回-1。每次读取字节的最大数量由数组长度决定。
FileOutputStream.write(buffer,0,length):
三个参数,第一个参数为字节数组,是将要写入的字符内容;第二个参数为整形数据,是写入数据开始的数组下标;第三个参数为整形数据,是写入数据需要写入的字符数量。无返回值。

FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
    fileInputStream = new FileInputStream("张子凡.jpg");
    fileOutputStream = new FileOutputStream("back.jpg");
    byte[] buffer = new byte[1024];
    int length = -1;
    while ((length = fileInputStream.read(buffer)) != -1) {
        ileOutputStream.write(buffer,0,length);
        System.out.println(buffer);
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException E) {
    E.printStackTrace();
} finally {
    if(fileOutputStream != null) {
        try {
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if(fileInputStream != null) {
        try {
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

四、对象文件读写

对象文件读写类ObjectOutputStream和ObjectInputStream无法直接创建能链接文件路径的对象,他们的的引用实例化对象依赖于字节文件读写类的实例化而创建。

1、写入

ObjectOutputStream.writeObject(student):
一个参数,参数类型为Object类型(任意引用类型),是要写入文件的对象内容。无返回值。

ObjectOutputStream objectOutputStream = null;
FileOutputStream fileOutputStream = null;
try {
    fileOutputStream = new FileOutputStream("stu");
    objectOutputStream = new ObjectOutputStream(fileOutputStream);
    Student student = new Student(1, "sa", 12, "male");
    objectOutputStream.writeObject(student);
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if(objectOutputStream != null) {
        try {
            objectOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if(fileOutputStream != null) {
        try {
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2、读出

objectInputStream.readObject():
无参数;返回值类型为Object(任意引用类型),直接返回读取到的对象。

ObjectInputStream objectInputStream = null;
FileInputStream fileInputStream = null;
try {
    fileInputStream = new FileInputStream("stu");
    objectInputStream = new ObjectInputStream(fileInputStream);
    Student s = (Student)objectInputStream.readObject();
    System.out.println(s);
} catch (FileNotFoundException | ClassNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if(objectInputStream != null) {
        try {
            objectInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if(fileInputStream != null) {
        try {
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flww*星火燎原

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值