IO流 方法引用 函数式接口

导读:本篇文章讲解 IO流 方法引用 函数式接口,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

一、IO流

1.理解

传输数据、做输入输出、拷贝、上传下载

不同流:一连串流动的数据,以先入先出的方式流动,管道

作业:传输数据,读入写出,上传下载

2.流的分类

1)按照流向分: 都是大脑为中心,程序为中心

输出流、输入流

文件(数据源)–输入流–>程序(目的地) 程序–输出->文件

2)按照操作数据单元:

字节流:万能流,能够传输任意类型的数据

字符流:纯文本内容

3)按照功能分:

节点流:真实用来传输数据,数据从数据源到目的地

功能流:扩展节点流的功能

3.字节流

读入 以程序为中心 数据源–读入–>程序

  • InputStream 抽象父类 字节输入流

  • FileInputStream 文件字节输入流,从系统文件中读入数据到程序

  • 构造器 FileInputStream(String name) 内部会先构建File对象,然后调用下面结构File对象的构造器

  • new FileInputStream(File);

  • 方法

  • read()->返回值int,读到的字节数据,如果没有数据返回-1 读入一个字节的数据

  • read(byte[]) 一个字节数组一个字节数组读入,返回读入到数组中数据的个数,如果没有读到,返回-1。

  • 注意:java中字符串中表示路径可以使用\或者/,使用\会识别成为转义字符

    public class IODemo01 {
    ​
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            //创建一个文件字节流
            FileInputStream file=new FileInputStream("F:\\test.txt");
            //System.out.println(file);
            //读取文件信息一个字节
    //      int num=file.read();
    //      System.out.println((char)num);
            //用循环来读取每个字节,循环为-1的时候结束
            int num=-1;
            while((num=file.read())!=-1){
                System.out.print((char)num);
            }
            //读取文件信息以数组输出
            byte[] by=new byte[1024];
            //文件有多少个字节
            int len=-1;
            while((len=file.read(by))!=-1){
                System.out.println(new String(by,0,len));
            }
            file.close();
        }
    }

     

字节输出流:

  • OutputStream 抽象父类

  • FileOutputStream 文件字节输出流

  • 构造器

  • FileOutputStream(String name) 创建一个文件输出流写入文件指定名称。

  • 默认覆盖原有内容

  • FileOutputStream(String name, boolean append)写出 并且追加。

  • 常用方法

  • write(int)

  • write(byte[] b)

  • write(byte[] b, int off, int len)

  • flush() 只要是输出流都要记得关闭之前刷出

  • close()

  • 写出的时候,如果文件系统中不存在,系统会自动帮你创建一个,但是如果是目录不存在,系统不会创建

    public class IODemo02 {
    ​
        public static void main(String[] args) throws IOException, InterruptedException {
            // TODO Auto-generated method stub
            //文件输出流,追加blooean ture(append)
            FileOutputStream os=new FileOutputStream("F://test.txt",true);
            //写入write(int)
            os.write(97);
            //write(byte[] b)
            os.write("hhhh".getBytes());
            //刷出
            os.flush();
            //关闭
            os.close();
        }
    }

     

5.文件拷贝字符流

数据源文件—–>程序——>目的地文件

  • 文件拷贝:

  • 1.创建流

  • 文件字节输入流

  • 文件字节输出流

  • 2.先读入 后写出

  • 3.刷出

  • 4.关闭(后打开的先关闭)

    public class CopyFile03 {
        public static void main(String[] args) {
            //1.创建流
            InputStream is=null;
            OutputStream os=null;
            try {
                /*is=new FileInputStream("D:/test.txt");
                os=new FileOutputStream("E:/test.txt");*/
                //2.先读入 后写出
                byte[] car=new byte[1024];
                int len=-1;
                while(-1!=(len=is.read(car))){
                    os.write(car, 0, len);
                }
                //3.刷出
                os.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally{ //无论try中是否出现异常代码,都会执行finally中的关闭功能
                //4.关闭(后打开的先关闭)
                try {
                    if(os!=null){  //预防空指针异常出现
                        os.close();
                    }
                    if(is!=null){  //预防空指针异常出现
                        is.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            
        }
    }
    ​

     

6.字符流

字符流:

  • 只能拷贝纯文本的数据

  • 输入流: Reader FileReader read() read(char[]) close()

  • 输出流: Writer FileWriter write() + flush() close()

  • 实现字符流的文件拷贝,测试拷贝图片不合适

    public class IODemo03 {
    ​
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            //字符流  Reader Writer
            //输入流
            Reader rd=new FileReader("F://test.txt");
            //Reader rd=new FileReader("D://1.jpg");
            //写入流
            //Writer rt=new FileWriter("D://3.jpg");
            Writer rt=new FileWriter("F://hh.txt");
            //读写
            char [] car=new char[1024];
            int len=-1;
            while((len=rd.read(car))!=-1){
                rt.write(car, 0, len);
            }
            //刷出
            rt.flush();
            //关闭
            rt.close();
            rd.close();
        }
    }

     

7.对象流(功能流)

对象流 (功能流) :读写对象|任意类型数据 保留数据+数据类型

  • 使用: 功能流(节点流)

  • 功能流:提高节点流的功能,增强性能

  • ObjectInputStream 对象|反序列化输入流 新增方法:readXxx()

  • ObjectOutputStream 对象|序列化字节输出流 新增方法:writeXxx()

  • 注意:不能发生多态

  • 序列化:把对象数据转为可存储或者可传输的过程,成为序列化

  • 不是所有的类型都能够序列化 java.io.Serializable

  • 先序列化后反序列化

  • 读写的内容顺序保持一致

  • 不是所有的属性都需要序列化 transient,属性值为默认值

  • 静态的内容不会被序列化

  • 如果父类实现序列化接口,子类的内容也可以序列化,子类实现序列化接口,子类只能序列化子类的内容,父类的内容无法序列化

    public class ObjectDemo01 {
    ​
        public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
            // TODO Auto-generated method stub
            write("F://test.txt");
            read("F://test.txt");
        }
        //序列化输出流   name:目的地文件的路径
        public static void write(String name) throws FileNotFoundException, IOException{
            //定义对象流输出流
            ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream(name));
            Student s=new Student("xiao",18);
            //写出
            os.writeBoolean(true);
            os.writeUTF("UTF-8");
            os.writeObject(s);
            //刷出
            os.flush();
            //关闭
            os.close();
            s.age=20;
        }
        //反序列化输入流
        public static void read(String name) throws FileNotFoundException, IOException, ClassNotFoundException{
            ObjectInputStream is=new ObjectInputStream(new FileInputStream(name));
            //读入
            boolean b=is.readBoolean();
            String str=is.readUTF();
            Object s=is.readObject();
            System.out.println(b+"--->"+str+"--->"+s);
            //关闭
            is.close();
        }
    ​
    }

     

二、函数式接口

1.四大函数式接口

消费性接口 Consumer<T> 表示接受单个输入参数并且不返回结果的操作。 void accept(T t) lambda表达式:T什么型都可以,有参数没有返回值 public static void get(String str,Consumer<Stirng>con){ con.accept(str); } //String get(“jlk”,t->t.方法()); String::equals//类::成员方法 //int get(2,t->{System.out.println(t);}) System.out::println//对象::成员方法 //boolean 供给型接口 Supplier<T> 该供应商提供的结果类型* T get()** get()方法在list集合中使用 public static List<Integer> get(int num,Supplier<List<Integer>>sup){ List ls=new ArrayList(); for(int i=0;i<num;i++){ ls.add(sup.get()); } return list; } //get(1,()->(int)Math.random()(5-2+1)+2); // 函数型接口 Function<T,R> 表示接受一个参数并产生结果的函数。** R apply(T t)** public static String get(String s1,Function<String,String>fun){

        return fun.apply(s2);;
    }
    get("jkk",t->t.方法)
    String::trim//类名::成员方法

断定型接口 Predicate<T> 表示一个参数的谓词(布尔值函数)。** boolean test(T t)** public static boolean get(String s,Predicate<String>pre){ return pre.test(s); } get(“”,t->t.方法()) String::equals //类名::成员方法

2.方法引用::

  • 简化Lambda表达式,可以理解为Lambda表达式的另一种表现形式

  • 前提: 当重写的抽象方法方法体的实现,其实就是通过调用另外一个已有的方法进行实现,可以通过方法引用进行调用,简化Lambda的写法

  • 对象的引用 ::成员方法;

  • 类名::静态方法名;

  • 类名::成员方法;

  • 要求: 函数型接口的抽象方法的参数列表和返回值要求与内部引用的方法的参数列表和返回值保持一致!!!!

    构造器引用:

  • new Person();

  • Person::new;

  • 类型::new;

    /**
     * 对象的引用 ::成员方法;
     * */
    public class Dome01 {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Fruit f=new Fruit();
            A<Fruit> a=()->{f.getName();};
            //对象调用了getName()
            a=f::getName;
            //System.out.println();
        }
    }
    class Fruit{
        private String name;
        public Fruit(){
            
        }
        public  void getName(){
            System.out.println("名字是:"+this.name);
        }
    }
    //创建接口
    interface A<T>{
        void get();
    }
    ​
    /**
     * 类名::静态方法名;
     * */
    public class Demo02 {
    ​
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            B<Integer> b=(t)->{Animal.getAge(t);};
            b=Animal::getAge;
        }
    }
    class Animal{
        int age;
        public Animal(){
            
        }
        public static void getAge(int age){
            System.out.println("年龄:"+age);
        }
    }
    interface B<T>{
        void get(T t);
    }
    /**
     * 构造器
     * 类型类名::new;
     * */
    public class Demo03 {
    ​
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Car car=new Car();
            C<Car> c=()->{return new Car();};
            c=()->new Car();
            c=Car::new;
        }
    ​
    }
    class Car{
        String name="str";
        public Car(){
            
        }
        
    }
    interface C<T>{
        T get();
    }
    ​
    //类名::静态方法
    public class Demo04 {
    ​
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Student s=new Student();
            D<Student,String> d=(t)->{return t.get(s);};
            d=Student::get;
        }
    ​
    }
    class Student{
        String name;
        public Student(){
            
        }
        public static String get(Student s){
            return s.name;
            
        }
    }
    interface D<T,R>{
        R get(T t);
    }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/121489.html

(0)
seven_的头像seven_bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!