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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
public class FileTest{
    public static void main(String[] args){
        
        // 파일명 선언
        String file_name = "test.txt";
 
        // 파일 선언
        File beforeFile = null;
        File afterFile = null;
 
        // 파일 읽고 쓰기 위한 FileInputStream, FileOutputStream 선언
        FileInputStream beforeStream = null;
        FileOutputStream afterStream = null;
 
        // 파일을 옮기기 위한 FileChannel 선언
        FileChannel fCBefore = null;
        FileChannel fCAfter = null;
 
        try{
 
            // 옮기기 전 절대경로 설정
            String before_path = "C:\\before\";
            // 옮긴 후 절대경로 설정
            String after_path = "C:\\after\";
 
            // 옮기기 전 파일객체 생성
            beforeFile = new File(before_path + file_name);
            // 옮긴 후 파일객체 생성
            afterFile = new File(after_path + file_name);
 
            // 옮기기 전 파일이 없을 경우
            if(!beforeFile.isFile()){
                System.out.println("파일이 존재하지 않습니다.");
            }
 
            File afterDir = new File(after_path);
 
            // 옮겨질 폴더가 없을 경우 폴더 생성
            if(!afterDir.exists()){
                afterDir.mkdirs();
            }
 
            beforeStream = new FileInputStream(before_path + file_name);
            afterStream = new FileInputStream(after_path + file_name);
 
            fCBefore = beforeStream.getChannel();
            fCAfter = afterStream.getChannel();
 
            // 옮기기 전 파일 사이즈
            long size = fCBefore.size();
 
            // 파일 옮기기
            fCBefore.transferTo(0, size, fCAfter);
 
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            try{
                if(fCAfter != null) fCAfter.close();
                if(fCBefore != null) fCBefore.close();
                if(afterStream != null) afterStream.close();
                if(beforeStream != null) beforeStream.close();
            }
            catch(IOException e){
                e.printStackTrace();
            }
        }
    }
}
cs