zoukankan      html  css  js  c++  java
  • 使用函数式接口来传递行为

    import java.io.BufferedReader;
    import java.io.IOException;
    
    @FunctionalInterface
    public interface BufferedReaderProcessor {
        String process(BufferedReader b) throws IOException;
    }
    
    
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class ProcessFile {
        //读取一行数据
        public static String processFile() throws IOException{
            try(BufferedReader br = new BufferedReader(new FileReader("data.txt"))){
                return br.readLine();
            }
        }
        //如果想读多行,使用函数式接口来传递行为
        public static String processFile(BufferedReaderProcessor p) throws IOException{
            try(BufferedReader br = new BufferedReader(new FileReader("data.txt"))){
                return p.process(br);//执行一个行为
            }
        }
        public static void main(String[] args) throws IOException {
            System.out.println(processFile());
            String oneLines = processFile((BufferedReader br)->br.readLine());//传递Lambda
            System.out.println(oneLines);
            String twoLines = processFile((BufferedReader br)->br.readLine()+" "+br.readLine());
            System.out.println(twoLines);
        }
    }
    
  • 相关阅读:
    TiDB架构特性
    TiDB入门
    ansible安装nginx
    linux命令之cowsay
    KeepAlived 搭建高可用的HAProxy集群
    HAProxy 实现镜像队列的负载均衡
    RabbitMQ高可用集群介绍
    docker安装phpMyAdmin
    centos7安装RabbitMQ
    Vim轻量级查看Java代码
  • 原文地址:https://www.cnblogs.com/fly-book/p/12637180.html
Copyright © 2011-2022 走看看