+++ /dev/null
-/** @file\r
- \r
- The file is used to provides interfaces for file operations \r
- \r
- Copyright (c) 2006, Intel Corporation\r
- All rights reserved. This program and the accompanying materials\r
- are licensed and made available under the terms and conditions of the BSD License\r
- which accompanies this distribution. The full text of the license may be found at\r
- http://opensource.org/licenses/bsd-license.php\r
- \r
- THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
- \r
- **/\r
-package org.tianocore.frameworkwizard.common;\r
-\r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.io.FileOutputStream;\r
-import java.io.InputStream;\r
-\r
-public class FileOperation {\r
-\r
- /**\r
- \r
- @param args\r
- * @throws Exception \r
- \r
- **/\r
- public static void main(String[] args) throws Exception {\r
- FileOperation.newFolder("C:\\aaa\\aaa\\aaa\\aaa\\aaa");\r
- }\r
-\r
- /**\r
- To new a folder\r
- \r
- @param folderPath The folder path to be created\r
- @throws Exception\r
- \r
- **/\r
- public static void newFolder(String folderPath) throws Exception {\r
- folderPath = Tools.convertPathToCurrentOsType(folderPath);\r
- File f = new File(folderPath);\r
- f.mkdirs();\r
- }\r
-\r
- /**\r
- Delete a file \r
- \r
- @param filePath The file path to be deleted\r
- @throws Exception\r
- \r
- **/\r
- public static void delFile(String filePath) throws Exception {\r
- File f = new File(filePath);\r
- if (f.exists()) {\r
- f.delete();\r
- }\r
- }\r
-\r
- /**\r
- Delete a folder and all its files\r
- \r
- @param filePath The name of the folder which need be deleted \r
- @throws Exception\r
- \r
- **/\r
- public static void delFolder(String filePath) throws Exception {\r
- File f = new File(filePath);\r
- if (!f.exists()) {\r
- return;\r
- }\r
- if (!f.isDirectory()) {\r
- return;\r
- }\r
- delFolder(f);\r
- }\r
-\r
- /**\r
- Delete a folder and all its files\r
- \r
- @param fleFolderName The name of the folder which need be deleted\r
- \r
- @retval true - Delete successfully\r
- @retval false - Delete successfully\r
- \r
- **/\r
- private static boolean delFolder(File fileName) throws Exception {\r
- boolean blnIsDeleted = true;\r
-\r
- File[] aryAllFiles = fileName.listFiles();\r
-\r
- for (int indexI = 0; indexI < aryAllFiles.length; indexI++) {\r
- if (blnIsDeleted) {\r
- if (aryAllFiles[indexI].isDirectory()) {\r
- //\r
- //If is a directory, recursively call this function to delete sub folders\r
- //\r
- blnIsDeleted = delFolder(aryAllFiles[indexI]);\r
- } else if (aryAllFiles[indexI].isFile()) {\r
- //\r
- //If is a file, delete it\r
- //\r
- if (!aryAllFiles[indexI].delete()) {\r
- blnIsDeleted = false;\r
- }\r
- }\r
- }\r
- }\r
- if (blnIsDeleted) {\r
- fileName.delete();\r
- }\r
- return blnIsDeleted;\r
- }\r
-\r
- /**\r
- Copy a file\r
- \r
- @param oldPath\r
- @param newPath\r
- @throws Exception\r
- \r
- **/\r
- public static void copyFile(String oldPath, String newPath) throws Exception {\r
- oldPath = Tools.convertPathToCurrentOsType(oldPath);\r
- newPath = Tools.convertPathToCurrentOsType(newPath);\r
- \r
- int byteCount = 0;\r
- File oldFile = new File(oldPath);\r
- \r
- File newFile = new File(Tools.getFilePathOnly(newPath));\r
- if (!newFile.exists()) {\r
- newFolder(Tools.getFilePathOnly(newPath));\r
- }\r
-\r
- if (oldFile.exists()) {\r
- InputStream is = new FileInputStream(oldPath);\r
- FileOutputStream fos = new FileOutputStream(newPath);\r
- byte[] buffer = new byte[1024];\r
-\r
- while ((byteCount = is.read(buffer)) != -1) {\r
- fos.write(buffer, 0, byteCount);\r
- }\r
-\r
- is.close();\r
- }\r
- }\r
-\r
- /**\r
- Copy a folder\r
- \r
- @param oldPath\r
- @param newPath\r
- @throws Exception\r
- \r
- **/\r
- public static void copyFolder(String oldPath, String newPath) throws Exception {\r
- File oldFile = new File(oldPath);\r
-\r
- //\r
- // Create new file path first\r
- //\r
- newFolder(newPath);\r
-\r
- String[] files = oldFile.list();\r
- File temp = null;\r
- for (int index = 0; index < files.length; index++) {\r
- if (oldPath.endsWith(DataType.FILE_SEPARATOR)) {\r
- temp = new File(oldPath + files[index]);\r
- } else {\r
- temp = new File(oldPath + DataType.FILE_SEPARATOR + files[index]);\r
- }\r
-\r
- if (temp.isFile()) {\r
- FileInputStream fis = new FileInputStream(temp);\r
- FileOutputStream fos = new FileOutputStream(newPath + DataType.FILE_SEPARATOR\r
- + (temp.getName()).toString());\r
- byte[] b = new byte[1024 * 5];\r
- int len;\r
- while ((len = fis.read(b)) != -1) {\r
- fos.write(b, 0, len);\r
- }\r
- fos.flush();\r
- fos.close();\r
- fis.close();\r
- }\r
- if (temp.isDirectory()) {\r
- copyFolder(oldPath + DataType.FILE_SEPARATOR + files[index], newPath + DataType.FILE_SEPARATOR\r
- + files[index]);\r
- }\r
- }\r
- }\r
-}\r