자바(Java)

[java] 자바 파일 복사 메소드 년월일 붙여서

xemaker 2024. 7. 19. 15:34

자바 파일 복사하는 메소드를 구현해보겠다. 여러 방법이 있지만 가장 간단하게 구현해보겠다.

원본 파일이 있고 원본파일을 파일명 뒤에 년월일을 붙여서 복사해서 리턴해주는 메소드 이다. 그래야 그 파일을 활용한 후 필요없으면 삭제하든지 할 수 있기 때문이다.

public static String fileCopy() throws IOException{
LocalDate now=LocalDate.now();
  DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyyMMdd");
  String fNow=now.format(formatter);

  String ori="D:\\a.pptx";
  String desc="D:\\a_"+fNow+".pptx";

  File file=new File(ori);
  File newFile=new File(dest);

  Files.copy(file.toPath(), newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);

  return dest;

}