【IO流,如果都能看懂,证明你理解了IO流】

本文介绍了Java中的IO流概念,包括字节流与字符流的区别,重点解析了RandomAccessFile类的功能与用法,并展示了如何使用不同类型的输入输出流进行文件读写操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Java流的概述:

1、Java中,所有输入流类都是抽象类inputStream字节输入流和抽象类Reader字符流的子类,而所有的输出流都是抽象类Output Stream字节输出流和Writer字符输出流的子类。

Reader类是字符流输入流的抽象类,所有字符输入流的实现都是它的子类

2、在Java中,输出流是OutPut StreamWriter两个类Writer类是字符输出流的抽象类,所有字符输出流的实现都是它的子类

流是一组有序的输出序列数据

二、RandomAccessFile

重点

想比较之前的流要么读要么写,RandomAccessFile提供了读写操作RandomAccessFile声明在java.io包下,但直接继承于java.lang.Object类。并且它实现了Datalnput、DataOutput两个接口

1、getFilePointer和seek是RandomAccessFile实现随机访问的核心方法
2、RandomAccessFile使用了指针的思想
3、RandomAccessFile可以用来实现多线程下载及断点续传

三、输入流和输出流

1、

public class Input {
    public static void main(String[] args) {
        char input = '\n';
                                              
        System.out.println("请开始输入:");
        StringBuilder sb = new StringBuilder();
        do {
                                                       
            try {
                                                       
                input = (char) System.in.read();
                sb.append(input);
                                                       
            } catch (Exception e) {
                                                       
                e.printStackTrace();
                                                       
            }
        } while (input != '\n');
                                                       
        System.out.println("请输入的字符串是:" + sb);
    }
}

2、


import java.util.Scanner;
                                         
public class Input {
    public static void main(String[] args) {
                                               
        System.out.print("请开始输入:");
        Scanner scan = new Scanner(System.in);
        String read = scan.nextLine();                                      
        System.out.println("请输入的字符串是:"+read);
    }
}

3、

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Input {
                                                                                 
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = null;
                                                                                 
        System.out.print("输入数据:");
        try {
                                                                                 
            input = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
                                                                                 
        } 
                                                                                 
        System.out.println("输入数据:" + input);
    }
}

四、文件输入流和输出流

1、FileRead

import java.io.*;

public class FileRead {
    public static void main(String[] args) throws IOException {
        String filePath = "test.txt";
        File file = new File(filePath);
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String lineString = null;
        while ((lineString = reader.readLine()) != null) {
            System.out.println(lineString);
        }
        reader.close();
    }
}

2、FileWriter类是从OutputStreamWriter继承而来

五、对象序列化

import java.io.*;
import java.io.Serializable;

                                                                                                                                         
class Student implements Serializable {
    private String name;
    private String sex;
    private Integer age;

    public Student(String name, String sex, Integer age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    @Override
    public String toString() {
        return "name:" + this.name + "," + "sex:" + this.sex + "," + "age:" + this.age;
    }
}

public class SerializableDemo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        // 序列化
        Student student = new Student("小明", "男", 15);
        System.out.println(student);
                                                                                                                                         
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student"));
        oos.writeObject(student);
        oos.close();
        // 反序列化
        File file = new File("student");
                                                                                                                                         
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        Student student = (Student) ois.readObject();
        ois.close();
        System.out.println(student);
    }
}

六、ZIP文件压缩

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Zip {
    public static void main(String[] args) throws IOException {
        String zipFileName = "test.zip";
        String fileName = "test.txt";
        File file = new File(fileName);
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
        out.putNextEntry(new ZipEntry(""));
        FileInputStream in = new FileInputStream(file);
        int tmp;
        while((tmp = in.read()) != -1) {
            out.write(tmp);
        }
        in.close();
        out.close();
    }
}

七、字节流:InputStream和Output Stream

1、input stream 和outputstream为各种输入和输出字节流的基类,所有字节流都继承这两个基类
2、其他的输入和输出流:
对文件的字节流操作:File Input Stream和FileOutStream
数据输入和输出流:Data Input Stream和DataOutputStream
带缓冲区的输入流和输出流:BufferedInputStream和BufferedOutputStream

字符流: InputStreamReader 和 OutputStreamWriter

1、 InputStreamReader 和 OutputStreamWriter为各种输入输出字符流的基类,所有字符流都继承这两个基类
2、其他输入和输出流:
对读取文件操作系统的封装:FileReader和FileWriter
BufferedReader和BufferedWriter

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

胖天才小朱(怀玉)

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

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

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

打赏作者

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

抵扣说明:

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

余额充值