【JavaOOP应用】3、文件读写--IO流、缓冲流、序列化与反序列化

3. 文件读写–IO流

3.1 File 类基本用法

3.1.1 File 类操作文件
/**
 * File类操作文件
 */
@Test
public void test01() throws IOException {
    // 1、定义一个文件对象(重点)
    File file = new File("src\\com\\hzit\\abc.txt");
    if (!file.exists()){
        // 2、创建新文件
        file.createNewFile();
    }
    // 3、获取文件大小(重点)
    long length = file.length();
    System.out.println("文件大小:" + length + "bytes");
    // 4、文件是否可读、可写、可执行
    boolean canRead = file.canRead();
    boolean canWrite = file.canWrite();
    boolean canExecute = file.canExecute();
    System.out.println("canRead = " + canRead);
    System.out.println("canWrite = " + canWrite);
    System.out.println("canExecute = " + canExecute);
    // 5、判断是否是文件(重点)
    boolean isFile = file.isFile();
    System.out.println("isFile = " + isFile);
    // 6、判断是否是目录(重点)
    boolean isDirectory = file.isDirectory();
    System.out.println("isDirectory = " + isDirectory);
    // 7、获取文件的绝对路径(重点)
    String absolutePath = file.getAbsolutePath();
    System.out.println("absolutePath = " + absolutePath);
    // 8、获取文件名(重点)
    String name = file.getName();
    System.out.println("name = " + name);
    // 9、获取文件所在的父目录(重点)
    String parent = file.getParent();
    System.out.println("parent = " + parent);
    // 10、获取此文件所在的磁盘的剩余空间
    long freeSpace = file.getFreeSpace();
    System.out.println("freeSpace = " + freeSpace / 1024 / 1024 / 1024 + "GB.");
    // 11、获取此文件所在磁盘的总空间大小
    long totalSpace = file.getTotalSpace();
    System.out.println("totalSpace = " + totalSpace / 1024 / 1024 / 1024 + "GB.");
    // 12、移动文件
    file.renameTo(new File("src\\com\\hzit\\test\\cba.txt"));
    // 13、删除文件
    file.delete();
}
3.1.2 File类操作文件夹(目录)
/**
     * File类操作文件夹(目录)
     */
@Test
public void test02(){
    // 1、定义一个目录
    File dir = new File("src\\com\\hzit\\file");
    // 2、判断是否是目录
    if (!dir.exists()){
        // 3、创建一级目录(重点)
        //            dir.mkdir();
        // 4、创建多级目录(重点)
        dir.mkdirs();
    }
    // 5、判断是否是目录
    boolean directory = dir.isDirectory();
    System.out.println("directory = " + directory);
    // 6、定义一个要遍历的目录
    File dirs = new File("src\\com\\hzit");
    // 7、将指定目录的子目录及文件名放到数组中(不常用)
    String[] dirNames = dirs.list();
    for (String dirName : dirNames) {
        System.out.println(dirName);
    }
    System.out.println("-----------------------------");
    // 8、将指定目录中的子目录及文件对象放到File数组中(经常用)
    File[] files = dirs.listFiles();
    for (File file : files) {
        String absolutePath = file.getAbsolutePath();
        System.out.println(absolutePath + ":" + file.length() + "bytes.");
    }
}

3.2 字节流用法

3.2.1 使用 FileInputStream 进行文件读取

字节输入流—>InputStream–>FileInputStream

/**
 * 将文件中的内容写入到程序中
 */
@Test
public void test() throws IOException {
    // 定义文件对象
    File file = new File("src\\com\\hzit\\txt\\bb.txt");
    // 定义一个输入流对象
    FileInputStream fileInputStream = new FileInputStream(file);
    // 将文件中的内容读取放入数组中
    byte[] b = new byte[(int) file.length()];
    fileInputStream.read(b);
    // 将字节数组的内容转成字符串输出
    String s = new String(b);
    System.out.println("s = " + s);
    // 关闭流
    fileInputStream.close();
}
3.2.2 使用 FileOutputStream 进行文件写入

字节输出流—>OutputStream–>FileOutputStream

/**
 * 将程序中的内容写到文件中
 */
@Test
public void test() throws IOException {
    // 定义要写入的文件对象
    File file = new File("com\\hzit\\txt\\bb.txt");
    // 定义一个输出流对象
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    // 定义写入文件的内容
    String s = "天空一声巨响,逗比闪亮登场!";
    // 将内容转化成字符数组
    byte[] bytes = s.getBytes();
    // 将内容写入文件
    fileOutputStream.write(bytes);
    // 关闭流
    fileOutputStream.close();
}

3.3 字符流用法

3.3.1 使用 FileReader 读取文本文件

符输入流—>Reader–>FileReader

/**
 * 将文件中的内容写入到程序中
 */
@Test
public void test() throws IOException {
    // 定义文件对象
    File file = new File("src\\com\\hzit\\txt\\bb.txt");
    // 定义一个输入流对象
    FileReader fileReader = new FileReader(file);
    // 将文件中的内容读取放入数组中
    char[] b = new char[(int) file.length()];
    fileReader.read(b);
    // 将字符数组的内容转成字符串输出
    String s = new String(b);
    System.out.println("s = " + s);
    // 关闭流
    fileReader.close();
}
3.3.2 使用 FileWriter 写入文本文件

符输出流—>Writer–>FileWriter

/**
 * 将程序中的内容写到文件中
 */
@Test
public void test() throws IOException {
    // 定义要写入的文件对象
    File file = new File("src\\com\\hzit\\txt\\dd.txt");
    // 定义一个输出流对象
    FileWriter fileWriter = new FileWriter(file, true);
    // 定义写入文件的内容
    String s = "呔!妖孽,哪里跑!";
    // 将内容写入文件
    fileWriter.write(s);
    // 关闭流
    fileWriter.close();

}

4. 文件读写–缓冲流

BufferedInputStream、BufferedOutputStream

4.1 字节缓冲流用法

逐字节读取 mp3 文件,效率很低:

/**
 * 1. 逐字节复制一个mp3文件,需要的时间。
 */
@Test
public void test01() throws IOException {
    // 定义要读取的文件
    File file = new File("F:\\music\\周传雄 - 冬天的秘密.kgm");
    // 定义写入目标文件的路径
    File file1 = new File("F:\\copy\\abc.kgm");
    // 定义文件输入流
    FileInputStream fileInputStream = new FileInputStream(file);
    // 定义文件输出流
    FileOutputStream fileOutputStream = new FileOutputStream(file1);
    // 定义读取到的一个字节变量
    int len = 0;
    while ((len = fileInputStream.read()) != -1){
        fileOutputStream.write(len);
    }
    // 关闭流
    fileInputStream.close();
    fileOutputStream.close();
}

逐字节读取 mp3 文件,采用缓冲流效率很高:

/**
 * 2. 逐字节复制一个mp3文件,需要的时间。( 字节缓冲流 )
 */
@Test
public void test02() throws IOException {
    // 定义要读取的文件
    File file = new File("F:\\music\\周传雄 - 冬天的秘密.kgm");
    // 定义写入目标文件的路径
    File file1 = new File("F:\\copy\\aaa.kgm");
    // 定义文件输入流
    FileInputStream fileInputStream = new FileInputStream(file);
    // 定义文件输出流
    FileOutputStream fileOutputStream = new FileOutputStream(file1);
    // 定义字节缓冲输入流和缓存输出流
    BufferedInputStream inputStream = new BufferedInputStream(fileInputStream);
    BufferedOutputStream outputStream = new BufferedOutputStream(fileOutputStream);
    // 定义读取到的一个字节变量
    int len = 0;
    while ((len = inputStream.read()) != -1){
        outputStream.write(len);
    }
    // 关闭流
    inputStream.close();
    outputStream.close();
}

小结:
1、缓冲流默认的缓冲区大小为 8KB。
2、当缓冲区大小没被读写的数据填满时,不会主动将数据写出到目标文件中,我们可以通过调用其 flush()方法或 close()来强制性将缓冲区的内容写出到文件中。实际上,close()方法中调用的就是 flush()方法,缓冲流其实还是将数据写到字节数组中。

4.2 字符缓冲流用法

/**
 * 字符缓冲流(BufferedReader + PrintWriter)
 * 案例一:将用户输入的信息写出到文本文件中,以end结束!
 */
private static void test03() throws IOException {
    // 得到键盘输入流对象
    InputStream in = System.in;
    // 将字节流转换成字符流
    InputStreamReader reader = new InputStreamReader(in);
    // 定义一个字符输入缓冲流
    BufferedReader bufferedReader = new BufferedReader(reader);
    // 循环输入信息,写入到指定文件中
    // 定义输出的目标文件
    File file = new File("com\\hzit\\txt\\input.txt");
    // 定义输出缓冲流
    FileWriter fileWriter = new FileWriter(file,true);
    PrintWriter printWriter = new PrintWriter(fileWriter, true);
    while (true){
        // 读取一行数据
        String str = bufferedReader.readLine();
        if ("end".equalsIgnoreCase(str)){
            break;
        }
        printWriter.println(str);
    }
    // 关闭流
    bufferedReader.close();
    printWriter.close();
}

5. 文件读写–序列化与反序列化

5.1 序列化

/**
     * 序列化(将对象写入到文件中)
     */
@Test
public void test01() throws IOException {
    // 构造要序列化的学生对象
    Student s1 = new Student(1001, "张三", "男", 20, "上海");
    Student s2 = new Student(1002, "李中", "男", 23, "杭州");
    Student s3 = new Student(1003, "王五", "男", 21, "苏州");
    // 定义要存放学生的文件对象
    File file = new File("src\\com\\hzit\\txt\\stu.txt");
    // 定义文件输出流
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    // 定义序列化对象
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
    // 将学生序列化到文件中
    objectOutputStream.writeObject(s1);
    objectOutputStream.writeObject(s2);
    objectOutputStream.writeObject(s3);
    // 关闭流
    objectOutputStream.close();
}

5.2 反序列化

/**
     * 2. 反序列化对象
     */
@Test
public void test02() throws IOException, ClassNotFoundException {
    // 定义需要读取的文件
    File file = new File("src\\com\\hzit\\txt\\stu.txt");
    // 定义一个反序列化对象
    ObjectInputStream stream = new ObjectInputStream(new FileInputStream(file));
    // 读取文件中的对象
    for (int i = 0; i < 3; i++) {
        Object object = stream.readObject();
        System.out.println(object);
    }
    // 关闭流
    stream.close();
}
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

浮生146

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

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

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

打赏作者

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

抵扣说明:

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

余额充值