Java I/O流

Chtholly 发布于 2022-08-27 1007 次阅读


按方向分类:

  • 输入流:将<存储设备>中的内容读取到<内存>中
  • 输出流:将<内存>中的内容写入到<存储设备中>
  • 文件 ----输入流---->程序----输出流---->文件

按单位分类;

  • 字节流:以字节为单位,可以读写所有数据
  • 字符流:以字符为单位,只能读写文本

按功能分类:

  • 节点流:具有实际传输数据的读写功能
  • 过滤流:在节点流的基础之上增强功能

字节流

FileInputStream

public static void main(String[] args) throws Exception {
        String path = "IOTest/Test01.txt";

        //创建FileInputStream,指定文件路径
        FileInputStream fis = new FileInputStream(path);

        //读取文件
        //fis.read();
        //单个字节读取
        /*int data;
        while ((data = fis.read())!=-1){
            System.out.print((char) data);
        }*/
        //Test01 HelloWorld

        //多个字节读取
        byte[] buffer = new byte[1024];//每次读取的字节长度
        int len;
        while ((len = fis.read(buffer))!=-1){
            System.out.println(new String(buffer,0,len));
        }
        //Test01 HelloWorld

        fis.close();
    }

FileOutputStream

public static void main (String[] args) throws Exception {
        FileOutputStream fos = new FileOutputStream("IOTest/Test02.txt");
        String data = "HelloWorld";
        fos.write(data.getBytes());
        fos.close();
    }

案例

//使用文件字节流实现文件的复制
public static void main(String[] args) throws Exception{
        FileInputStream fis = new FileInputStream("IOTest/TestOld03.jpg");
        FileOutputStream fos = new FileOutputStream("IOTest/TestNew03.jpg");

        //缓冲数组,数组大小即为每次读取的字节数(每次读取1024个字节)
        byte[] buf = new byte[1024];
        //用来存储每次读取的字节数组buf的长度(最后的读取buf数组可能少于1024)
        int len;
        while ((len = fis.read(buf)) != -1){
            //write方法将buf数组中的内容写入文件,从0开始,长度为len
            fos.write(buf,0,len);
        }

        System.out.println("复制完毕");

        fis.close();
        fos.close();
    }

字节缓冲流

  • 提高IO效率,减少磁盘的访问次数
  • 数据存储在缓冲区中,flush是将缓冲区的内容写入文件中,也可以直接close

BufferedInputStream

public static void main(String[] args) throws Exception{
        //创建节点流,即实际操作文件的流
        FileInputStream fileInputStream = new FileInputStream("IOTest/Test01.txt");
        //创建过滤流,在为节点流增强功能
        BufferedInputStream bis = new BufferedInputStream(fileInputStream);

        //缓冲流自带8k的缓冲区
        /*int data = 0;
        while ((data = bis.read()) != -1){
        //强转char会导致中文乱码
            System.out.print((char)data);
        }*/
        byte[] buf = new byte[1024];
        int len = 0;
        while ((len = bis.read(buf)) != -1){
            System.out.print(new String(buf,0,len));
        }

        bis.close();
    }

BufferedOutputStream

public static void main(String[] args) throws Exception{
        FileOutputStream fileOutputStream = new FileOutputStream("IOTest/Test03.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
        String data = "松下问童子,\n" +
                      "言师采药去。\n" +
                      "只在此山中,\n" +
                      "云深不知处。";
        bos.write(data.getBytes());
        bos.flush();//将缓存区中的数据刷新到硬盘
        bos.close();//close()方法会自动调用flush()方法
    }

对象流

  • 包括ObjectOutputStream/ObjectInputStream
  • 增强了缓冲区功能
  • 增强了读写8种基本数据类型和字符串功能
  • 增强了读写对象的功能

使用流传输对象的过程称为序列化和反序列化

ObjectOutputStream 序列化

//学生类
//可序列化的类必须实现Serializable接口,该接口没有方法,仅作标记该类可序列化用
public class Student implements Serializable{
    private String name;
    private int age;

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public static void main(String[] args) throws Exception{
        //对象流
        FileOutputStream fileOutputStream = new FileOutputStream("IOTest/stu.bin");
        ObjectOutputStream oos = new ObjectOutputStream(fileOutputStream);

        //序列化
        Student student1 = new Student("张三",21);
        Student student2 = new Student("李四",22);
        Student student3 = new Student("王五",20);

        ArrayList<Student> studentArrayList = new ArrayList<>();
        studentArrayList.add(student1);
        studentArrayList.add(student2);
        studentArrayList.add(student3);

        oos.writeObject(studentArrayList);

        oos.close();
    }

ObjectInputStream 反序列化

public static void main(String[] args) throws Exception{
        FileInputStream fileInputStream = new FileInputStream("IOTest/stu.bin");
        ObjectInputStream ois = new ObjectInputStream(fileInputStream);

        ArrayList<Student> arrayList = (ArrayList<Student>) ois.readObject();

        System.out.println(arrayList);

        ois.close();
    }

序列化与反序列化注意事项

  • 序列化类必须要实现Serializable接口
  • 序列化类中对象属性也要求实现Serializable接口
  • 序列化版本号ID serialVerisonUID,保证序列化的类和反序列化的类是同一个类
  • 使用transien修饰属性,这个属性不能序列化
  • 静态属性不能被序列化
  • 序列化多个对象,可以借助集合实现

字符流

FileReader

public static void main(String[] args) throws Exception{
        FileReader fileReader = new FileReader("IOTest/Test03.txt");

        /*int data = 0;
        while ((data = fileReader.read()) != -1){
            System.out.print((char)data);
        }*/

        char[] buf = new char[1024];
        int len;
        while ((len = fileReader.read(buf)) != -1){
            System.out.println(new String(buf,0,len));
        }

        fileReader.close();
    }

FileWriter

public static void main(String[] args) throws Exception{
        FileWriter fileWriter = new FileWriter("IOTest/writer.txt");

        String string = "松下问童子,\n" +
                "言师采药去。\n" +
                "只在此山中,\n" +
                "云深不知处。";
        fileWriter.write(string);

        fileWriter.close();
    }

案例

//使用字符流复制文件,只能复制文本文件
    public static void main(String[] args) throws Exception{
        FileReader fileReader = new FileReader("IOTest/writer.txt");
        FileWriter fileWriter = new FileWriter("IOTest/writerNew.txt");

        char[] buf = new char[1024];
        int len = 0;
        while ((len = fileReader.read(buf)) != -1){
            fileWriter.write(buf,0,len);
        }

        System.out.println("执行完毕");

        fileReader.close();
        fileWriter.close();
    }

字符缓冲流

  • 提高字符流读写功能
  • 高效读写
  • 支持输入换行符
  • 可一次写一行、读一行

BufferedReader

public static void main(String[] args) throws Exception{
        FileReader fileReader = new FileReader("IOTest/Test01.txt");
        BufferedReader br = new BufferedReader(fileReader);

        /*char[] buf = new char[1024];
        int len;
        while ((len = br.read(buf)) != -1){
            System.out.println(new String(buf,0,len));
        }*/

        //System.out.println(br.readLine());

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

        br.close();
    }

BufferedWriter

public static void main(String[] args) throws Exception{
        FileWriter fileWriter = new FileWriter("IOTest/Test11.txt");
        BufferedWriter bw = new BufferedWriter(fileWriter);

        String data="松下问童子,\n" +
                "言师采药去。\n" +
                "只在此山中,\n" +
                "云深不知处。";

        bw.write(data);

        bw.close();
    }

打印流

PrinterWriter,将数据原样打印至目的文件,支持的数据类型更加丰富

public static void main(String[] args) throws Exception{
        PrintWriter pw = new PrintWriter("IOTest/test.txt");

        pw.println(97);
        pw.println(true);
        pw.println(3.14);
        pw.println('a');

        pw.close();
    }

转换流

  • 桥转换流:InputStreamReader/OutputStreamWriter
  • 可将字节流转换为字符流
  • 可设置字符的编码方式

InputStreamReader

public static void main(String[] args) throws Exception{
        FileInputStream fis = new FileInputStream("IOTest/writer.txt");
        InputStreamReader isr = new InputStreamReader(fis,"utf-8");

        int data;
        while ((data = isr.read()) != -1){
            System.out.print((char)data);
        }

        isr.close();
    }

OutputStreamWriter

public static void main(String[] args) throws Exception{
        FileOutputStream fos = new FileOutputStream("IOTest/test1.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");

        osw.write("我爱java");

        osw.close();
    }

File类

概念:待物物理盘符中的一个文件或者文件夹

方法:

  • createNewFile( ) 创建一个新文件
  • mkdir( ) 创建一个新目录
  • delete( ) 删除文件或目录
  • exists( ) 判断File对象所代表的对象是否存在
  • getAbsolutePath( ) 获取文件的绝对路径
  • getName( ) 获取文件名
  • getParent( ) 获取文件/目录所在的目录
  • isDirectory( ) 判断是否是目录
  • isFile( ) 判断是否是文件
  • length( ) 文件长度
  • listFiles( ) 列出目录中的所有内容
  • renameTo( ) 修改文件名
public class Test16 {
    public static void main(String[] args) throws Exception{
        //sqparator();
        //路径分隔符;
        //名称分隔符\

        //fileOpe();

        directoryOpe();

    }
    //1、分隔符
    public static void sqparator(){
        System.out.println("路径分隔符"+ File.pathSeparator);
        System.out.println("名称分隔符"+File.separator);
    }
    //2、文件操作
    public  static void fileOpe() throws Exception{
        //创建文件
        File file = new File("IOTest/file.txt");
        if(!file.exists()){
            boolean isT = file.createNewFile();
            System.out.println("创建结果 "+isT);
        }
        //获取文件信息
        System.out.println("获取文件绝对路径:"+file.getAbsolutePath());
        System.out.println("获取文件路径:"+file.getPath());
        System.out.println("获取文件名称:"+file.getName());
        System.out.println("获取文件父目录:"+file.getParent());
        System.out.println("获取文件长度:"+file.length());
        System.out.println("获取文件创建时间:"+new Date(file.lastModified()));
        //判断
        System.out.println("是否可写:"+file.canWrite());
        System.out.println("是否是文件:"+file.isFile());
        System.out.println("是否隐藏:"+file.isHidden());
        // 删除文件
        //直接删除
        //System.out.println("删除结果:"+file.delete());
        //使用jvm退出时删除
        file.deleteOnExit();
    }
    //文件夹操作
    public static void directoryOpe(){
        //创建文件夹
        File dir = new File("IOTest/dir/test01/mkdir");
        System.out.println(dir);
        if(!dir.exists()){
            //dir.mkdir(); 创建单级目录
            System.out.println("创建结果:"+dir.mkdirs());
        }
        //获取文件夹信息
        System.out.println("获取文件夹绝对路径:"+dir.getAbsolutePath());
        System.out.println("获取文件夹路径:"+dir.getPath());
        System.out.println("获取文件夹名称:"+dir.getName());
        System.out.println("获取文件夹父目录:"+dir.getParent());
        System.out.println("获取文件夹长度:"+dir.length());
        System.out.println("获取文件夹创建时间:"+new Date(dir.lastModified()));
        //判断
        System.out.println("是否可写:"+dir.canWrite());
        System.out.println("是否是文件夹:"+dir.isFile());
        System.out.println("是否隐藏:"+dir.isHidden());
        //遍历文件夹
        File dir2 = new File("IOTest\\dir\\suijixinhai");
        String[] strings = dir2.list();
        for(String string : strings){
            System.out.println(string);
        }
        //FileFilter接口
        File[] files = dir2.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                if(pathname.getName().endsWith(".jpg")){
                    return true;
                }
                return false;
            }
        });
        //删除
        //只能删除最后一级的空目录,需要先将目录中的文件全部删除
        dir.delete();
        dir.deleteOnExit();
    }
}

案例

public class Test17 {
    public static void main(String[] args) throws Exception{
        listDir(new File("IOTest/dir"));
        deleteDir(new File("IOTest/dir"));
    }
    //递归遍历文件夹
    public static void listDir(File dir){
        File[] files = dir.listFiles();
        System.out.println(dir.getAbsolutePath());
        if(files!=null && files.length>0){
            for(File file : files){
                if(file.isDirectory()){
                    listDir(file);
                }else {
                    System.out.println(file.getAbsolutePath());
                }
            }
        }
    }
    //递归删除文件夹
    public static void deleteDir(File dir){
        File[] files = dir.listFiles();
        if(files!=null && files.length>0){
            for(File file : files){
                if(file.isDirectory()){
                    deleteDir(file);
                }else {
                    System.out.println(file.getAbsolutePath()+"删除:"+file.delete());
                }
            }
        }
        System.out.println(dir.getAbsolutePath()+"删除:"+dir.delete());
    }
}

补充:ProPerties

Properties:属性集合

特点:

  • 存储属性名和属性值
  • 属性名和属性值都是字符串类型
  • 没有泛型
  • 和流有关
public static void main(String[] args) throws Exception{
        //创建集合
        Properties properties = new Properties();
        //添加数据
        properties.setProperty("username","张三");
        properties.setProperty("age","24");
        //遍历
        //keySet
        //entrySet
        //stringPropertyName
        Set<String> proname = properties.stringPropertyNames();
        for (String string : proname){
            System.out.println(string+":"+properties.getProperty(string));
        }
        //和流有关的方法
        //1、List方法
        PrintWriter pw = new PrintWriter("IOTest/pro.txt");
        properties.list(pw);
        pw.close();
        //2、store方法 保存
        FileOutputStream fos = new FileOutputStream("IOTest/store.properties");
        properties.store(fos,"first commit");
        fos.close();
        //3、load方法 加载
        FileInputStream fis = new FileInputStream("IOTest/store.properties");
        properties.load(fis);
        System.out.println(properties);
        fis.close();
    }

总结

流的概念:内存与存储设备之间传输数据的通道

流的分类:输入流、输出流;字节流、字符流;节点流、过滤流;

序列化、反序列化:将对象通过流写入到文件,或将对象通过流读取到内存,必须实现Serializable接口

File对象:代表物理盘符中的一个文件或文件夹

此作者没有提供个人介绍。
最后更新于 2022-08-28