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