]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/FileOperation.java
1. Change ToolCode from text field to drop down list, and user can enter their custom...
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / common / FileOperation.java
CommitLineData
a13899c5 1/** @file\r
2 \r
3 The file is used to provides interfaces for file operations \r
4 \r
5 Copyright (c) 2006, Intel Corporation\r
6 All rights reserved. This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10 \r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13 \r
14 **/\r
15package org.tianocore.frameworkwizard.common;\r
16\r
17import java.io.File;\r
18import java.io.FileInputStream;\r
19import java.io.FileOutputStream;\r
20import java.io.InputStream;\r
21\r
22public class FileOperation {\r
23\r
24 /**\r
25 \r
26 @param args\r
27 * @throws Exception \r
28 \r
29 **/\r
30 public static void main(String[] args) throws Exception {\r
79cb6fdb 31 FileOperation.newFolder("C:\\aaa\\aaa\\aaa\\aaa\\aaa");\r
a13899c5 32 }\r
33\r
34 /**\r
35 To new a folder\r
36 \r
37 @param folderPath The folder path to be created\r
38 @throws Exception\r
39 \r
40 **/\r
41 public static void newFolder(String folderPath) throws Exception {\r
42 folderPath = Tools.convertPathToCurrentOsType(folderPath);\r
a929458e 43 File f = new File(folderPath);\r
44 f.mkdirs();\r
a13899c5 45 }\r
46\r
47 /**\r
48 Delete a file \r
49 \r
50 @param filePath The file path to be deleted\r
51 @throws Exception\r
52 \r
53 **/\r
54 public static void delFile(String filePath) throws Exception {\r
55 File f = new File(filePath);\r
56 if (f.exists()) {\r
57 f.delete();\r
58 }\r
59 }\r
60\r
61 /**\r
62 Delete a folder and all its files\r
63 \r
64 @param filePath The name of the folder which need be deleted \r
65 @throws Exception\r
66 \r
67 **/\r
68 public static void delFolder(String filePath) throws Exception {\r
69 File f = new File(filePath);\r
70 if (!f.exists()) {\r
71 return;\r
72 }\r
73 if (!f.isDirectory()) {\r
74 return;\r
75 }\r
76 delFolder(f);\r
77 }\r
78\r
79 /**\r
80 Delete a folder and all its files\r
81 \r
82 @param fleFolderName The name of the folder which need be deleted\r
83 \r
84 @retval true - Delete successfully\r
85 @retval false - Delete successfully\r
86 \r
87 **/\r
88 private static boolean delFolder(File fileName) throws Exception {\r
89 boolean blnIsDeleted = true;\r
90\r
91 File[] aryAllFiles = fileName.listFiles();\r
92\r
93 for (int indexI = 0; indexI < aryAllFiles.length; indexI++) {\r
94 if (blnIsDeleted) {\r
95 if (aryAllFiles[indexI].isDirectory()) {\r
96 //\r
97 //If is a directory, recursively call this function to delete sub folders\r
98 //\r
99 blnIsDeleted = delFolder(aryAllFiles[indexI]);\r
100 } else if (aryAllFiles[indexI].isFile()) {\r
101 //\r
102 //If is a file, delete it\r
103 //\r
104 if (!aryAllFiles[indexI].delete()) {\r
105 blnIsDeleted = false;\r
106 }\r
107 }\r
108 }\r
109 }\r
110 if (blnIsDeleted) {\r
111 fileName.delete();\r
112 }\r
113 return blnIsDeleted;\r
114 }\r
115\r
116 /**\r
117 Copy a file\r
118 \r
119 @param oldPath\r
120 @param newPath\r
121 @throws Exception\r
122 \r
123 **/\r
124 public static void copyFile(String oldPath, String newPath) throws Exception {\r
79cb6fdb 125 oldPath = Tools.convertPathToCurrentOsType(oldPath);\r
126 newPath = Tools.convertPathToCurrentOsType(newPath);\r
127 \r
a13899c5 128 int byteCount = 0;\r
129 File oldFile = new File(oldPath);\r
79cb6fdb 130 \r
131 File newFile = new File(Tools.getFilePathOnly(newPath));\r
132 if (!newFile.exists()) {\r
133 newFolder(Tools.getFilePathOnly(newPath));\r
134 }\r
a13899c5 135\r
136 if (oldFile.exists()) {\r
137 InputStream is = new FileInputStream(oldPath);\r
138 FileOutputStream fos = new FileOutputStream(newPath);\r
139 byte[] buffer = new byte[1024];\r
140\r
141 while ((byteCount = is.read(buffer)) != -1) {\r
142 fos.write(buffer, 0, byteCount);\r
143 }\r
144\r
145 is.close();\r
146 }\r
147 }\r
148\r
149 /**\r
150 Copy a folder\r
151 \r
152 @param oldPath\r
153 @param newPath\r
154 @throws Exception\r
155 \r
156 **/\r
157 public static void copyFolder(String oldPath, String newPath) throws Exception {\r
158 File oldFile = new File(oldPath);\r
159\r
160 //\r
161 // Create new file path first\r
162 //\r
163 newFolder(newPath);\r
164\r
165 String[] files = oldFile.list();\r
166 File temp = null;\r
167 for (int index = 0; index < files.length; index++) {\r
168 if (oldPath.endsWith(DataType.FILE_SEPARATOR)) {\r
169 temp = new File(oldPath + files[index]);\r
170 } else {\r
171 temp = new File(oldPath + DataType.FILE_SEPARATOR + files[index]);\r
172 }\r
173\r
174 if (temp.isFile()) {\r
175 FileInputStream fis = new FileInputStream(temp);\r
176 FileOutputStream fos = new FileOutputStream(newPath + DataType.FILE_SEPARATOR\r
177 + (temp.getName()).toString());\r
178 byte[] b = new byte[1024 * 5];\r
179 int len;\r
180 while ((len = fis.read(b)) != -1) {\r
181 fos.write(b, 0, len);\r
182 }\r
183 fos.flush();\r
184 fos.close();\r
185 fis.close();\r
186 }\r
187 if (temp.isDirectory()) {\r
188 copyFolder(oldPath + DataType.FILE_SEPARATOR + files[index], newPath + DataType.FILE_SEPARATOR\r
189 + files[index]);\r
190 }\r
191 }\r
192 }\r
193}\r