]>
Commit | Line | Data |
---|---|---|
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 | |
15 | package org.tianocore.frameworkwizard.common;\r | |
16 | \r | |
17 | import java.io.File;\r | |
18 | import java.io.FileInputStream;\r | |
19 | import java.io.FileOutputStream;\r | |
20 | import java.io.InputStream;\r | |
21 | \r | |
22 | public 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 | |
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 | |
79cb6fdb | 48 | temp = Tools.addFileSeparator(temp);\r |
a13899c5 | 49 | folderPath = folderPath.substring(folderPath.indexOf(DataType.FILE_SEPARATOR) + DataType.FILE_SEPARATOR.length());\r |
50 | continue;\r | |
51 | }\r | |
79cb6fdb | 52 | temp = Tools.addFileSeparator(temp);\r |
a13899c5 | 53 | folderPath = folderPath.substring(folderPath.indexOf(DataType.FILE_SEPARATOR) + DataType.FILE_SEPARATOR.length()); \r |
54 | } else {\r | |
79cb6fdb | 55 | temp = Tools.addFileSeparator(temp) + folderPath;\r |
a13899c5 | 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 | |
79cb6fdb | 143 | oldPath = Tools.convertPathToCurrentOsType(oldPath);\r |
144 | newPath = Tools.convertPathToCurrentOsType(newPath);\r | |
145 | \r | |
a13899c5 | 146 | int byteCount = 0;\r |
147 | File oldFile = new File(oldPath);\r | |
79cb6fdb | 148 | \r |
149 | File newFile = new File(Tools.getFilePathOnly(newPath));\r | |
150 | if (!newFile.exists()) {\r | |
151 | newFolder(Tools.getFilePathOnly(newPath));\r | |
152 | }\r | |
a13899c5 | 153 | \r |
154 | if (oldFile.exists()) {\r | |
155 | InputStream is = new FileInputStream(oldPath);\r | |
156 | FileOutputStream fos = new FileOutputStream(newPath);\r | |
157 | byte[] buffer = new byte[1024];\r | |
158 | \r | |
159 | while ((byteCount = is.read(buffer)) != -1) {\r | |
160 | fos.write(buffer, 0, byteCount);\r | |
161 | }\r | |
162 | \r | |
163 | is.close();\r | |
164 | }\r | |
165 | }\r | |
166 | \r | |
167 | /**\r | |
168 | Copy a folder\r | |
169 | \r | |
170 | @param oldPath\r | |
171 | @param newPath\r | |
172 | @throws Exception\r | |
173 | \r | |
174 | **/\r | |
175 | public static void copyFolder(String oldPath, String newPath) throws Exception {\r | |
176 | File oldFile = new File(oldPath);\r | |
177 | \r | |
178 | //\r | |
179 | // Create new file path first\r | |
180 | //\r | |
181 | newFolder(newPath);\r | |
182 | \r | |
183 | String[] files = oldFile.list();\r | |
184 | File temp = null;\r | |
185 | for (int index = 0; index < files.length; index++) {\r | |
186 | if (oldPath.endsWith(DataType.FILE_SEPARATOR)) {\r | |
187 | temp = new File(oldPath + files[index]);\r | |
188 | } else {\r | |
189 | temp = new File(oldPath + DataType.FILE_SEPARATOR + files[index]);\r | |
190 | }\r | |
191 | \r | |
192 | if (temp.isFile()) {\r | |
193 | FileInputStream fis = new FileInputStream(temp);\r | |
194 | FileOutputStream fos = new FileOutputStream(newPath + DataType.FILE_SEPARATOR\r | |
195 | + (temp.getName()).toString());\r | |
196 | byte[] b = new byte[1024 * 5];\r | |
197 | int len;\r | |
198 | while ((len = fis.read(b)) != -1) {\r | |
199 | fos.write(b, 0, len);\r | |
200 | }\r | |
201 | fos.flush();\r | |
202 | fos.close();\r | |
203 | fis.close();\r | |
204 | }\r | |
205 | if (temp.isDirectory()) {\r | |
206 | copyFolder(oldPath + DataType.FILE_SEPARATOR + files[index], newPath + DataType.FILE_SEPARATOR\r | |
207 | + files[index]);\r | |
208 | }\r | |
209 | }\r | |
210 | }\r | |
211 | }\r |