프로그래밍 언어/Java
Java) 파일 전체 내용을 문자열(String)로 가져오기
seungyoon
2021. 7. 1. 10:50
// IOException 반드시 처리해줘야 함
public static String getTemplateFile() throws IOException{
// 파일 내용 전체를 담을 문자열
String contents = "";
// try (..) 이 안에서 파일을 열어주면 마지막에 finally 없어도 됨
try (FileReader fr = new FileReader("파일 경로/파일명");
BufferedReader br = new BufferedReader(fr);) {
String readLine = "";
while ((readLine = br.readLine()) != null) {
contents += readLine;
}
} catch (IOException e) {
e.printStackTrace();
}
return contents;
}