]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/FileOperation.java
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@671 6f19259b...
[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
31 FileOperation.newFolder("C:\\aaa\\bbb\\ccc\\ddd\\eee");\r
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
43 String temp = "";\r
44 while (folderPath.length() > 0) {\r
45 if (folderPath.indexOf(DataType.FILE_SEPARATOR) > -1) {\r
46 temp = temp + folderPath.substring(0, folderPath.indexOf(DataType.FILE_SEPARATOR));\r
47 if (temp.endsWith(":")) {\r
48 temp = temp + DataType.FILE_SEPARATOR;\r
49 folderPath = folderPath.substring(folderPath.indexOf(DataType.FILE_SEPARATOR) + DataType.FILE_SEPARATOR.length());\r
50 continue;\r
51 }\r
52 temp = temp + DataType.FILE_SEPARATOR;\r
53 folderPath = folderPath.substring(folderPath.indexOf(DataType.FILE_SEPARATOR) + DataType.FILE_SEPARATOR.length()); \r
54 } else {\r
55 temp = temp + DataType.FILE_SEPARATOR + folderPath;\r
56 folderPath = "";\r
57 }\r
58 File f = new File(temp);\r
59 if (!f.exists()) {\r
60 f.mkdir();\r
61 }\r
62 }\r
63 }\r
64\r
65 /**\r
66 Delete a file \r
67 \r
68 @param filePath The file path to be deleted\r
69 @throws Exception\r
70 \r
71 **/\r
72 public static void delFile(String filePath) throws Exception {\r
73 File f = new File(filePath);\r
74 if (f.exists()) {\r
75 f.delete();\r
76 }\r
77 }\r
78\r
79 /**\r
80 Delete a folder and all its files\r
81 \r
82 @param filePath The name of the folder which need be deleted \r
83 @throws Exception\r
84 \r
85 **/\r
86 public static void delFolder(String filePath) throws Exception {\r
87 File f = new File(filePath);\r
88 if (!f.exists()) {\r
89 return;\r
90 }\r
91 if (!f.isDirectory()) {\r
92 return;\r
93 }\r
94 delFolder(f);\r
95 }\r
96\r
97 /**\r
98 Delete a folder and all its files\r
99 \r
100 @param fleFolderName The name of the folder which need be deleted\r
101 \r
102 @retval true - Delete successfully\r
103 @retval false - Delete successfully\r
104 \r
105 **/\r
106 private static boolean delFolder(File fileName) throws Exception {\r
107 boolean blnIsDeleted = true;\r
108\r
109 File[] aryAllFiles = fileName.listFiles();\r
110\r
111 for (int indexI = 0; indexI < aryAllFiles.length; indexI++) {\r
112 if (blnIsDeleted) {\r
113 if (aryAllFiles[indexI].isDirectory()) {\r
114 //\r
115 //If is a directory, recursively call this function to delete sub folders\r
116 //\r
117 blnIsDeleted = delFolder(aryAllFiles[indexI]);\r
118 } else if (aryAllFiles[indexI].isFile()) {\r
119 //\r
120 //If is a file, delete it\r
121 //\r
122 if (!aryAllFiles[indexI].delete()) {\r
123 blnIsDeleted = false;\r
124 }\r
125 }\r
126 }\r
127 }\r
128 if (blnIsDeleted) {\r
129 fileName.delete();\r
130 }\r
131 return blnIsDeleted;\r
132 }\r
133\r
134 /**\r
135 Copy a file\r
136 \r
137 @param oldPath\r
138 @param newPath\r
139 @throws Exception\r
140 \r
141 **/\r
142 public static void copyFile(String oldPath, String newPath) throws Exception {\r
143 int byteCount = 0;\r
144 File oldFile = new File(oldPath);\r
145\r
146 if (oldFile.exists()) {\r
147 InputStream is = new FileInputStream(oldPath);\r
148 FileOutputStream fos = new FileOutputStream(newPath);\r
149 byte[] buffer = new byte[1024];\r
150\r
151 while ((byteCount = is.read(buffer)) != -1) {\r
152 fos.write(buffer, 0, byteCount);\r
153 }\r
154\r
155 is.close();\r
156 }\r
157 }\r
158\r
159 /**\r
160 Copy a folder\r
161 \r
162 @param oldPath\r
163 @param newPath\r
164 @throws Exception\r
165 \r
166 **/\r
167 public static void copyFolder(String oldPath, String newPath) throws Exception {\r
168 File oldFile = new File(oldPath);\r
169\r
170 //\r
171 // Create new file path first\r
172 //\r
173 newFolder(newPath);\r
174\r
175 String[] files = oldFile.list();\r
176 File temp = null;\r
177 for (int index = 0; index < files.length; index++) {\r
178 if (oldPath.endsWith(DataType.FILE_SEPARATOR)) {\r
179 temp = new File(oldPath + files[index]);\r
180 } else {\r
181 temp = new File(oldPath + DataType.FILE_SEPARATOR + files[index]);\r
182 }\r
183\r
184 if (temp.isFile()) {\r
185 FileInputStream fis = new FileInputStream(temp);\r
186 FileOutputStream fos = new FileOutputStream(newPath + DataType.FILE_SEPARATOR\r
187 + (temp.getName()).toString());\r
188 byte[] b = new byte[1024 * 5];\r
189 int len;\r
190 while ((len = fis.read(b)) != -1) {\r
191 fos.write(b, 0, len);\r
192 }\r
193 fos.flush();\r
194 fos.close();\r
195 fis.close();\r
196 }\r
197 if (temp.isDirectory()) {\r
198 copyFolder(oldPath + DataType.FILE_SEPARATOR + files[index], newPath + DataType.FILE_SEPARATOR\r
199 + files[index]);\r
200 }\r
201 }\r
202 }\r
203}\r