Java I/O流

Java I/O流

流是什么:

计算机中的流失一种信息的转换,它是一种有序流。
通常我们把对象接收外界的信息输入(Input)称为输入流
从对象向外输出(Output)称为输出流


一、IO流层次图

在这里插入图片描述

二、File类

2.1、描述

File类:文件和目录路径名的抽象表示。

2.2、File的常用方法

方法描述
getName()返回文件名称
getPath()返回相对路径
getAbsolutePath()返回绝对路径
length()返回文件大小,单位Byte
isFile()是否为文件,是返回true,否则返回false
isDirectory()是否为文件夹,是返回true,否则返回false
exists()是否存在
mkdir()创建文件夹
createNewFile()创建新文件
delete()删除文件

三、字节流操作文件

3.1 FileInputStream 读取文件(输入流)

public static void main(String[] args) {

        FileInputStream fis = null;

        try {
            fis = new FileInputStream("D:/2.txt");
            int a;
//            逐一读取赋值
//            while ((a = fis.read()) != -1) {
//                System.out.println((char) a);
//            }
//					直接赋值给byte数组  读取byte
            byte[] bytes = new byte[1024];
            while ((a = fis.read(bytes)) != -1) {
                for (int i = 0; i < bytes.length; i++) {
                    System.out.println((char) bytes[i]);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

3.2 FileOutputStream 写入文件(输出流)

public static void main(String[] args) {
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream("D:/2.txt",true);
            String a = "anshinengdie\n";
            fos.write(a.getBytes());
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

3.3 DataInputStream与DataOutputStream(用来操作二进制文件)

  • 注意:其实上方的FileInputStream与FileOutputStream也可以操作二进制文件。
public static void main(String[] args) {
        DataInputStream dis = null;
        FileInputStream fis = null;

        DataOutputStream dos = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream("./resources/aaa.jpg");
            dis = new DataInputStream(fis);

            fos = new FileOutputStream("aaacopy.jpg");
            dos = new DataOutputStream(fos);

            int temp;
            while ((temp = dis.read()) != -1) {
                dos.write(temp);
            }
            dos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                dos.close();
                fos.close();
                dis.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

四、字符流操作文件

4.1 FileRead 与 FileWriter

4.1.1 FileRead
public static void main(String[] args) {

        FileReader fr = null;
        try {
            fr = new FileReader("myPrim.txt");
/*            while (fr.read()!=-1){
                System.out.println((char) fr.read());
            }*/
            StringBuffer stringBuffer = new StringBuffer();
            char[] a = new char[1024];
            while (fr.read(a)!=-1){
                stringBuffer.append(a);
            }
            System.out.println(stringBuffer);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

4.1.2 FileWriter
public static void main(String[] args) {

        FileWriter fw = null;
//            获取系统的文件编码
        try {
            System.out.println(System.getProperty("file.encoding"));
            fw = new FileWriter("1.txt",true);
            String a = "aaa";
            fw.write(a);
            fw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

4.2BufferedRead 与 BufferedWriter

4.2.1 BufferedRead
public static void main(String[] args) {

        Reader fr = null;
        FileInputStream fis = null;
        BufferedReader br = null;

        try {
            fis = new FileInputStream("aaa/1.txt");
            fr = new InputStreamReader(fis,"UTF8");
            br = new BufferedReader(fr);
            String content = "";

            while ((content = br.readLine())!= null) {
                System.out.println(content);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                br.close();
                fr.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
4.2.1 BufferedWriter
public static void main(String[] args) {

        Writer fw = null;
        BufferedWriter bw = null;
        OutputStreamWriter osw = null;
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("aaa/1.txt",true);
            osw = new OutputStreamWriter(fos,"UTF8");
            bw = new BufferedWriter(osw);

            bw.write("今天最高温度是25度");
            bw.newLine();
            bw.write("走出去玩");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fos.flush();
                osw.flush();
                bw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

五、序列化

5.1描述

序列化:是将对象的状态写入到特定的流中的过程(将对象转化为字节)
反序列化:是从特定的流中获取数据重新构建对象的过程(将字节转化为对象)
在这里插入图片描述

5.2 序列化的实现

将对象实现Serializable接口
在这里插入图片描述

例如:

5.2.1 实现接口
import java.io.Serializable;
public class Student implements Serializable {
    String name;
    String gender;
    Integer age;

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

    public Student() {
    }

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

    public String getName() {
        return name;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
5.2.2 序列化(将对象转换成字节写入文件)
public static void main(String[] args) {
        Student student = new Student("zhangsan", "male", 21);
        System.out.println(student.getName());
        System.out.println(student.getAge());
        ObjectOutputStream oos = null;
        try {
            FileOutputStream fos = new FileOutputStream("./resources/stuseria.txt");
            oos = new ObjectOutputStream(fos);
            oos.writeObject(student);


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
5.3 反序列化(将文件二进制转化为对象)
public static void main(String[] args) {
//        Student student = new Student("zhangsan", "male", 21);
        ObjectInputStream ois = null;
        FileInputStream fis = null;

        try {
            fis = new FileInputStream("./resources/stuseria.txt");
            ois = new ObjectInputStream(fis);

            Student a =(Student) ois.readObject();

            System.out.println(a.gender);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                ois.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值