람다 식에 문제가 있습니다. 내 코드 :람다 식 처리 예외
public static void main(String[] args) throws IOException {
Function<String, String> lambda = path -> {
String result = null;
BufferedReader br = new BufferedReader(new FileReader(path));
String line;
while ((line = br.readLine()) != null) {
result = result + line;
}
return result;
};
}
그건 가능 :
public static void main(String[] args) {
Function<String, String> lambda = path -> {
String result = null;
try {
BufferedReader br = new BufferedReader(new FileReader(path));
String line;
while ((line = br.readLine()) != null) {
result = result + line;
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
};
}
은 그 같은 코드를 만들기 위해 지금 노력하고있어? java.util.function 만 사용할 수 있습니다. "lambda"에서 try catch를 삭제하려고하고 내 "main"메소드가 해당 Exception을 catch해야합니다.
참고 첫 번째 솔루션으로 메모리 누수를 일으키는 원인이되는'BufferedRead'을 닫습니다 결코. – Spotted