summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/FileOperation.java
blob: 7f15de8f8902daa45ea07f3c76939f1b642ed8fd (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/** @file
 
 The file is used to provides interfaces for file operations 
 
 Copyright (c) 2006, Intel Corporation
 All rights reserved. This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
 http://opensource.org/licenses/bsd-license.php
 
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 
 **/
package org.tianocore.frameworkwizard.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class FileOperation {

    /**
     
     @param args
     * @throws Exception 
     
     **/
    public static void main(String[] args) throws Exception {
        FileOperation.newFolder("C:\\aaa\\aaa\\aaa\\aaa\\aaa");
    }

    /**
     To new a folder
     
     @param folderPath The folder path to be created
     @throws Exception
     
     **/
    public static void newFolder(String folderPath) throws Exception {
        folderPath = Tools.convertPathToCurrentOsType(folderPath);
        File f = new File(folderPath);
        f.mkdirs();
    }

    /**
     Delete a file 
     
     @param filePath The file path to be deleted
     @throws Exception
     
     **/
    public static void delFile(String filePath) throws Exception {
        File f = new File(filePath);
        if (f.exists()) {
            f.delete();
        }
    }

    /**
     Delete a folder and all its files
     
     @param filePath The name of the folder which need be deleted 
     @throws Exception
     
     **/
    public static void delFolder(String filePath) throws Exception {
        File f = new File(filePath);
        if (!f.exists()) {
            return;
        }
        if (!f.isDirectory()) {
            return;
        }
        delFolder(f);
    }

    /**
     Delete a folder and all its files
     
     @param fleFolderName The name of the folder which need be deleted
     
     @retval true - Delete successfully
     @retval false - Delete successfully
     
     **/
    private static boolean delFolder(File fileName) throws Exception {
        boolean blnIsDeleted = true;

        File[] aryAllFiles = fileName.listFiles();

        for (int indexI = 0; indexI < aryAllFiles.length; indexI++) {
            if (blnIsDeleted) {
                if (aryAllFiles[indexI].isDirectory()) {
                    //
                    //If is a directory, recursively call this function to delete sub folders
                    //
                    blnIsDeleted = delFolder(aryAllFiles[indexI]);
                } else if (aryAllFiles[indexI].isFile()) {
                    //
                    //If is a file, delete it
                    //
                    if (!aryAllFiles[indexI].delete()) {
                        blnIsDeleted = false;
                    }
                }
            }
        }
        if (blnIsDeleted) {
            fileName.delete();
        }
        return blnIsDeleted;
    }

    /**
     Copy a file
     
     @param oldPath
     @param newPath
     @throws Exception
     
     **/
    public static void copyFile(String oldPath, String newPath) throws Exception {
        oldPath = Tools.convertPathToCurrentOsType(oldPath);
        newPath = Tools.convertPathToCurrentOsType(newPath);
        
        int byteCount = 0;
        File oldFile = new File(oldPath);
        
        File newFile = new File(Tools.getFilePathOnly(newPath));
        if (!newFile.exists()) {
            newFolder(Tools.getFilePathOnly(newPath));
        }

        if (oldFile.exists()) {
            InputStream is = new FileInputStream(oldPath);
            FileOutputStream fos = new FileOutputStream(newPath);
            byte[] buffer = new byte[1024];

            while ((byteCount = is.read(buffer)) != -1) {
                fos.write(buffer, 0, byteCount);
            }

            is.close();
        }
    }

    /**
     Copy a folder
     
     @param oldPath
     @param newPath
     @throws Exception
    
    **/
    public static void copyFolder(String oldPath, String newPath) throws Exception {
        File oldFile = new File(oldPath);

        //
        // Create new file path first
        //
        newFolder(newPath);

        String[] files = oldFile.list();
        File temp = null;
        for (int index = 0; index < files.length; index++) {
            if (oldPath.endsWith(DataType.FILE_SEPARATOR)) {
                temp = new File(oldPath + files[index]);
            } else {
                temp = new File(oldPath + DataType.FILE_SEPARATOR + files[index]);
            }

            if (temp.isFile()) {
                FileInputStream fis = new FileInputStream(temp);
                FileOutputStream fos = new FileOutputStream(newPath + DataType.FILE_SEPARATOR
                                                            + (temp.getName()).toString());
                byte[] b = new byte[1024 * 5];
                int len;
                while ((len = fis.read(b)) != -1) {
                    fos.write(b, 0, len);
                }
                fos.flush();
                fos.close();
                fis.close();
            }
            if (temp.isDirectory()) {
                copyFolder(oldPath + DataType.FILE_SEPARATOR + files[index], newPath + DataType.FILE_SEPARATOR
                                                                             + files[index]);
            }
        }
    }
}