9.Java----IO流

1.png

1.IO流的分类

  • 根据处理数据类型的不同分为:字符流和字节流
  • 根据数据流向不同分为:输入流和输出流


    2.png

2.节点流和处理流

3.jpg

3.常用例子

3.1基本分为4个步骤:

1.创建File对象
2.创建流对象
3.进行读/写操作
4.关闭流对象

//抽象基类         节点流                                   缓冲流(处理流的一种)
//InputStream      FileInputStream(字节流,图片,视频等)       BufferedInputStream
//OutputStream     FileOutputStream                             BufferedOutputStream
//Reader           FileReader(字符流,处理txt等文本文件)       BufferedReader
//Writer           FileWriter                                   BufferedWriter
public class FileTest {
    //FileReader
    @Test
    public void test1() {
        FileReader reader = null;
        try {
            //实例化文件
            File file = new File("./helloworld.txt");
            //创建读入/写入对象
            reader = new FileReader(file);
            //读入/写入
            //int read = reader.read();
            //方式一:
//            int read;
//            while ((read = reader.read()) != -1){
//                System.out.print((char)read);
//            }
            //方式二:
            int len;
            char[] chars = new char[5];
            while ((len = reader.read(chars)) != -1) {
                //len表示读取的字符数,保存在chars数组中
                for (int i = 0; i < len; i++) {
                    System.out.print(chars[i]);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭
            try {
                if (reader != null)
                    reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    //FileWriter
    @Test
    public void test2() {
        FileWriter fw = null; //如果append为true,则在原文件中追加,否则覆盖文件
        try {
            File file = new File("./helloword2.txt");
            fw = new FileWriter(file, false);
            Writer a = fw.append('a');
            fw.write("helloworld2!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fw != null)
                    fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    //复制
    @Test
    public void test3() {
        FileReader fr = null;
        FileWriter fw = null;
        try {
            File file = new File("helloworld.txt");
            File file1 = new File("copy_helloword.txt");

            fr = new FileReader(file);
            fw = new FileWriter(file1);

            char[] chars = new char[1];
            int len;
            while ((len = fr.read(chars)) != -1) {
                System.out.println(chars[0]);
                fw.write(chars, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //图片复制 FileOutputWriter,FileInputReader
    @Test
    public void test4() {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File src = new File("./pic.png");
            File dest = new File("./pic2.png");

            fis = new FileInputStream(src);
            fos = new FileOutputStream(dest);

            byte[] bytes = new byte[1024];
            int len;

            while ((len = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    //使用缓冲流(内部有个有容器,当读取到一次数量的数据时,一次性写入)
    @Test
    public void test5(){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        FileInputStream fis;
        FileOutputStream fos;
        try {
            File src = new File("./pic.png");
            File dest = new File("./pic3.png");
            //创建节点流
            fis = new FileInputStream(src);
            fos = new FileOutputStream(dest);
            //使用处理流来包裹节点流
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            byte[] bytes = new byte[1024];
            int len;

            while((len = bis.read(bytes)) != -1){
                bos.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //只要关闭外层的流,内层的流会自动关闭
            if(bis !=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    //使用对象流
    @Test
    public void test6(){
        try {
            File file = new File("object.txt");
            //序列化,将对象写入文件中
            //1.类必须实现Serializable接口
            //2.需要一个UID,如果没有的话,运行时会自动生成一个,但是当类改变时,反序列化时会出现错误
            // private static final long serialVersionUID = -6849794470754667710L;
            FileOutputStream fos = new FileOutputStream(file,false);

            ObjectOutputStream oos = new ObjectOutputStream(fos);

            oos.writeObject(new Person("yang",18));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test7(){
        try {
            File file = new File("./object.txt");
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            Person person = (Person) ois.readObject();
            System.out.println(person);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

class Person implements Serializable{
    String name;
    int age;

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

    public Person(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;
    }


}

4.自我总结

要进行IO的操作,首要要确定是要进行读操作还是写操作,读的话就是Input/Reader,写的话就是Output/Writer,其次确定操作的是字符还是字节,字符流的话就是使用Reader,字节流的话就是使用Stream,同时在使用readf()进行读操作时,使用char[] 或 byte[] 数组来存储每一次读入的数据,然后使用write()将char[] 或 byte[]中的数据写入指定文件中。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 五、IO流 1、IO流概述 (1)用来处理设备(硬盘,控制台,内存)间的数据。(2)java中对数据的操作都是通过...
    佘大将军阅读 534评论 0 0
  • tags:io categories:总结 date: 2017-03-28 22:49:50 不仅仅在JAVA领...
    行径行阅读 2,227评论 0 3
  • 一、基础知识:1、JVM、JRE和JDK的区别:JVM(Java Virtual Machine):java虚拟机...
    杀小贼阅读 2,417评论 0 4
  • 《夜静思》 —春节又逢元宵佳节有感 耳冲烟花爆竹声, 游人踏雪赏花灯。 佳节逢春元宵夜, 亲朋贺岁诉衷情。...
    双一悦阅读 189评论 0 1
  • 3月上中旬坚持练习数学题,发现很多的数学问题自己都绕不过来,数学计算容易错,因此以后要多进行计算练习。 完成了“小...
    冰清玉洁志高人杰阅读 297评论 0 8