@FunctionalInterface
interface FileHandle {
void doSome(String fileContent);
}
public class LamdaTest {
//能够了解为一种匿名函数的代替
//可选参数类型
//合乎lamda表达式的函数时接口:只有一个形象办法
//函数式接口注解@FunctionInterface 非必要
//函数式接口的形象办法签名:函数描述符
public static void handleFileContent(String url,FileHandle fileHandle) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader(url));
String line;
StringBuilder stringBuilder = new StringBuilder();
while ((line = bufferedReader.readLine())!= null) {
stringBuilder.append(line + '\n');
}
fileHandle.doSome(stringBuilder.toString());
}
public static void main(String[] args) throws IOException {
LamdaTest.handleFileContent("D:\\hello.txt",str -> System.out.println(str.toUpperCase()));
}
}
发表回复