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