Java IO流(详解)

目录

1.概述

2.File文件类

2.1 文件的创建操作

2.2 文件的查找操作

3. File里面一些其他方法

3.1 经典案例

4. IO流

4.1 概念

4.2 IO分类

4.3 字节输出流

4.4 字节输入流

4.5 案例

4.6 字符输出流

4.7 字符输入流

4.8 案例

4.9 处理流--缓冲流

4.10 对象流:


1.概述

在学习io流之前,我们需要先学习File文件类。因为它只能对文件进行操作,但是无法对文件中的内容进行操作

2.File文件类

在java jdk关于对文件(目录和文件)的操作都封装到File类。该类中包含了文件的各种属性以及操作方法。该类放在jdk-java.io包下。

2.1 文件的创建操作

 public static void main(String[] args) throws IOException {
//        //创建一个文件对象--参数文件的路径--绝对路径和相对路径
//        File file=new File("D:\\qy174.txt");
//        //相对路径--当前工程
//        File file02=new File("qy174.txt");
//        //调用创建功能--文件
//        file.createNewFile();
//        file02.createNewFile();

//        File file=new File("D://gyh//zql//ldh");
//        //创建目录
//        //file.mkdir();
//        //创建多级目录 /a/b/c/d
//        file.mkdirs();

        //删除文件或目录
//        File file02=new File("D:\\aaaaaaa");
//        //删除目录时只能删除空目录
//        file02.delete();

        //修改文件名称
        File file02=new File("qy174.txt");
        File file01=new File("zhenshuai.txt");
        file02.renameTo(file01);
    }

2.2 文件的查找操作

public class Test02 {
    public static void main(String[] args) {
        File file01=new File("D:\\aaaaaaa");
//        File file01=new File("zhenshuai.txt");
        //获取文件名
        String name = file01.getName();
        System.out.println("文件名:"+name);
        //获取文件的父路径
        String parent = file01.getParent();
        System.out.println("父路径:"+parent);
        //获取文件相对路径
        String path = file01.getPath();
        System.out.println("文件路径:"+path);
        //绝对路径
        String absolutePath = file01.getAbsolutePath();
        System.out.println(absolutePath);
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        //获取该目录下所有的子文件名
        String[] list = file01.list();
        for(String s:list){
            System.out.println(s);
        }
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        //获取当前目录下的所有子文件对象
        File[] files = file01.listFiles();
        for(File f:files){
            String absolutePath1 = f.getAbsolutePath();
            System.out.println(absolutePath1);
        }
    }
}

3. File里面一些其他方法

//判断文件或目录是否存在。
        boolean exists = file01.exists();
        System.out.println("文件是否存在"+exists);
        //判断文件是否为目录
        boolean directory = file01.isDirectory();
        System.out.println("该文件是否为目录:"+directory);

3.1 经典案例

查询某个目录下所有的文件。

public static void main(String[] args) {
        File file=new File("D:\\gz01-spring-transaction01");
        listFile(file);
    }
    public static void listFile(File file){
        //判断file是否为目录
        boolean directory = file.isDirectory();
        if(directory){
            //查看该目录下所有的文件对象
            File[] files = file.listFiles();
            //遍历
            for (File f:files){
                //递归调用
                listFile(f);
            }
        }else{
            System.out.println("文件名:"+file.getAbsolutePath());
        }
    }

4. IO流

4.1 概念

上面讲解得到File,它只能对文件进行操作,但是无法对文件中的内容进行操作。如果需要对文件中的内容进行操作则需要IO流。IO针对文件的内容操作的一个类集合

4.2 IO分类

流的方向: 输入流和输出流。

 根据流的内容: 字节流和字符流。

4.3 字节输出流

字节输出流的父类:OutputStream,它是所有字节输出流的父类,它是一个抽象类,最常见的子类FileOutputStream。

public static void main(String[] args) {
        //创建一个字节输出流--往zhenshuai.txt文件中输出内容---该流默认覆盖原本的内容
        OutputStream os = null;
        try {
            os = new FileOutputStream(new File("src/zhenshuai.txt"), true);
            String msg = "曹荣华也很真帅";
            //调用输出功能---必须为字节类型--转化为字节
            os.write(msg.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭流
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

4.4 字节输入流

以字节的方式读取文件中的内容。读取到程序中【内存中】。 字节输入流的父类InputStream,它里面包含操作字节的方法, 它也是一个抽象类,最常见的子类:FileInputStream。

public class Test02 {
    public static void main(String[] args) throws Exception {
        //创建一个字节输入流对象
        InputStream is=new FileInputStream(new File("src/zhenshuai.txt"));
        //调用读取的方法--每次读取一个字节--并把读取的内容返回--如果没有内容返回-1
        int a=-1;
        while ((a=is.read())!=-1){
            char c= (char) a;
            System.out.print(c);
        }
        is.close();
    }
}

4.5 案例

通过流完成文件的复制。

public class Test03 {
    public static void main(String[] args) throws Exception {
        //输入流
        InputStream is=new FileInputStream(new File("D:\\rabbitmq视频\\1.什么是mq.mp4"));
        //输出流
        OutputStream os=new FileOutputStream(new File("D:\\qy174.mp4"));
        int a=-1;
        while((a=is.read())!=-1){
             os.write(a);
        }
        is.close();
        os.close();
    }
}

4.6 字符输出流

Write类,它是所有字符输出流的父类,输出的内容以字符为单位。它下面常用的子类FileWrite。

public class Test04 {
    public static void main(String[] args) throws Exception {
        //创建一个字符输出流--如果文件不存在--该流回自动创建
        Writer w=new FileWriter(new File("liurui.txt"),true);
        String str="你好李焕英! 热辣滚烫";
        w.write(str);
        w.close();
    }
}

4.7 字符输入流

Reader它是字符输入流的一个父类,它也是抽象类,常见的子类FileReader.以字符为单位。

public class Test05 {
    public static void main(String[] args) throws Exception {
        Reader r=new FileReader(new File("liurui.txt"));
        //读取的单位为一个字符--如果没有内容返回的结果-1
        int i=-1;
        while ((i=r.read())!=-1){
            char c= (char) i;
            System.out.print(c);
        }
        r.close();

    }
}

4.8 案例

字符流它只能复制文本内容---无法复制文本之外的内容:比如视频,音频,图片 word。

public class Test06 {
    public static void main(String[] args) throws Exception{
        //输入流
        Reader r=new FileReader(new File("D:\\2.jpg"));
        //输出流
        Writer w=new FileWriter(new File("D:\\3.jpg"));

        int a=-1;
        while ((a=r.read())!=-1){
            w.write(a);
        }
        r.close();
        w.close();

    }
}

 字节流--可以操作任何文件的内容 字符流只适合操作文本的内容。

4.9 处理流--缓冲流

常用的缓冲流: BufferedInputStream 和BufferOutputStream 缓冲流是作用在流上。

public class Test07 {
    public static void main(String[] args)throws Exception {
        InputStream is=new FileInputStream(new File("liurui.txt"));
        //创建一个缓冲字节输入流
        BufferedInputStream bis=new BufferedInputStream(is);
        //缓冲流里面的方法操作文件内容
        int read =-1;
        while ((read=bis.read())!=-1){
            char c= (char) read;
            System.out.print(c);
        }
        bis.close();
        is.close();
    }
}
public class Test08 {
    public static void main(String[] args) throws Exception{
        OutputStream os=new FileOutputStream(new File("liurui.txt"));
        BufferedOutputStream bos=new BufferedOutputStream(os);
        String msg="hello";
        bos.write(msg.getBytes());

        bos.close();//刷新缓冲区
        os.close();
    }
}

提供效率:

public class Test09 {
    public static void main(String[] args) throws Exception {
        //开始时间
        long l = System.currentTimeMillis();
        //输入流
        InputStream is=new FileInputStream(new File("D:\\rabbitmq视频\\1.什么是mq.mp4"));
        BufferedInputStream bis=new BufferedInputStream(is);
        //输出流
        OutputStream os=new FileOutputStream(new File("D:\\qy174-02.mp4"));
        BufferedOutputStream bos=new BufferedOutputStream(os);
        int a=-1;
        while((a=bis.read())!=-1){
             bos.write(a);
        }
        bos.close();
        bis.close();
        is.close();
        os.close();
        //结束时间
        long l1 = System.currentTimeMillis();
        System.out.println("耗时:"+(l1-l));
    }
}

4.10 对象流:

操作java类对象。把java类对象写入到文件中,或者从文件中读取写入的java对象。

序列化: 把内存中的java对象写入到文件中的过程--序列化。

反序列化: 把文件中的对象读取到内存中---反序列化。

ObjectInputStream和ObjectOutputStream

ObjectOutputStream

把java对象写入到文件中。

public class Test10 {
    public static void main(String[] args) throws Exception{

        read();
    }
    public static void read() throws Exception{
        InputStream is=new FileInputStream(new File("liurui.txt"));
        ObjectInputStream ois=new ObjectInputStream(is);

        Object o = ois.readObject();

        ois.close();
        is.close();

        System.out.println(o);

    }
    //序列化过程: 借助ObjectOutputStream流 调用writeObject()  注意: 该类必须实现序列化接口Serializable
    public static void write() throws Exception{
        //把该对象写入liurui.txt文件中
        Student s=new Student(16,"闫克起");
        OutputStream os=new FileOutputStream(new File("liurui.txt"));
        ObjectOutputStream oos=new ObjectOutputStream(os);
        //把一个java对象写入文件时要求该对象所在的类必须实现序列化接口
        oos.writeObject(s);
        oos.close();
        os.close();

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

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

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

    public Student() {
    }
}

  • 18
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值