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);
}
}