티스토리 뷰

자바 원본 파일이 있고 파일명을 오늘날짜로 해서 복사하는 방법자바이다.

반복적이고 매일 파일명을 오늘날짜로 해서 올려야 하는곳에서 사용할 수 있다.

package dailycheck;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CopyFileTodaySample {
   
    public static final String path="D:\\";
    public static final String file1="AAA";
    public static final String ext=".pptx";
   
    public static void main(String[] args) {
       
        SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMdd");
       
        Date time = new Date();
       
        String fmtDate=format1.format(time);
        System.out.println(fmtDate);
       
        String oriFile1 = path+file1+ext;
        String destFile1 = path+file1+"_"+fmtDate+ext;
       
        System.out.println(oriFile1);
        System.out.println(destFile1);
       
        try{
            FileInputStream fis = new FileInputStream(oriFile1);
            FileOutputStream fos = new FileOutputStream(destFile1);
           
            int fileByte=0;
            while((fileByte = fis.read()) !=-1 ){
                fos.write(fileByte);
            }
           
            fis.close();
            fos.close();
           
        }catch(Exception e){
            e.printStackTrace();
        }
       
    }
}


D드라이브를 확인해 보면 파일이 오늘닐짜로 해서 복사 되어 있을 것이다.

AAA.pptx가 원본파일로 있고
AAA_20210728.pptx 파일이 복사 되어 있을 것이다


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함