00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 package org.openmobileis.common.util.file;
00030
00031 import java.io.*;
00032
00036 public class FileUtilities {
00037 private static final String emptyString = "";
00038
00043 public static String convertFileNameToSystem(String zipFile) {
00044 String ret =null;
00045 switch (File.separatorChar) {
00046 case '/' :
00047 ret = zipFile.replace('\\', '/');
00048 break;
00049 case '\\' :
00050 ret = zipFile.replace('/', '\\');
00051 break;
00052 default :
00053 ret = zipFile;
00054 break;
00055 }
00056
00057 return ret;
00058 }
00059
00060
00061
00062
00069 public static boolean deleteDir(File dir) {
00070 if (dir.isDirectory()) {
00071 String[] children = dir.list();
00072 for (int i=0; i<children.length; i++) {
00073 boolean success = deleteDir(new File(dir, children[i]));
00074 if (!success) {
00075 return false;
00076 }
00077 }
00078 }
00079 return dir.delete();
00080 }
00087 public static String getPathFromCompleteFileName(String completeName) {
00088 int index = completeName.lastIndexOf(File.separatorChar);
00089 if (index != -1) {
00090 return completeName.substring(0, index+1);
00091 } else {
00092 return emptyString;
00093 }
00094 }
00095
00102 public static String getNameFromCompleteFileName(String completeName) {
00103 int index = completeName.lastIndexOf(File.separatorChar);
00104 if (index == -1) {
00105 return completeName;
00106 } else {
00107 if (index != completeName.length()) {
00108 return completeName.substring(index+1, completeName.length());
00109 }
00110 }
00111 return emptyString;
00112 }
00113
00117 public static void moveFile(String oldFileName, String newFileName) {
00118 File newFile = new File(newFileName);
00119
00120 if (newFile.exists()) {
00121 newFile.delete();
00122 }
00123 File oldFile = new File(oldFileName);
00124 oldFile.renameTo(newFile);
00125 if (oldFile.exists()) {
00126 oldFile.delete();
00127 }
00128 }
00129
00130 public static String convertToUnixFomat(String filePath) {
00131 return filePath.replace('\\', '/');
00132 }
00133
00134 public static byte[] readFile(String completeFileName) throws FileNotFoundException, IOException {
00135 FileInputStream fileStream = new FileInputStream(completeFileName);
00136 try {
00137
00138 byte[] buffer = new byte[1024];
00139 ByteArrayOutputStream stream = new ByteArrayOutputStream(buffer.length);
00140 int bytes_read;
00141 try {
00142 while(( bytes_read = fileStream.read(buffer)) != -1)
00143 stream.write(buffer, 0, bytes_read);
00144 } finally {
00145 stream.flush();
00146 stream.close();
00147 }
00148 return stream.toByteArray();
00149 } finally {
00150 fileStream.close();
00151 }
00152 }
00153
00154 public static byte[] readFile(File file) throws FileNotFoundException, IOException {
00155 FileInputStream fileStream = new FileInputStream(file);
00156 try {
00157
00158 byte[] buffer = new byte[1024];
00159 ByteArrayOutputStream stream = new ByteArrayOutputStream(buffer.length);
00160 int bytes_read;
00161 try {
00162 while(( bytes_read = fileStream.read(buffer)) != -1)
00163 stream.write(buffer, 0, bytes_read);
00164 } finally {
00165 stream.flush();
00166 stream.close();
00167 }
00168 return stream.toByteArray();
00169 } finally {
00170 fileStream.close();
00171 }
00172 }
00173
00179 public static final void removeAllFile(File file, String ext, boolean recurcif) {
00180 if ((recurcif) &&(file.isDirectory())) {
00181 File[] fileList = file.listFiles();
00182 for (int i=0; i<fileList.length; i++) {
00183 removeAllFile(fileList[i], ext, recurcif);
00184 }
00185 }
00186 if (ext != null) {
00187 if (file.getName().indexOf(ext) != -1) {
00188 file.delete();
00189 }
00190 } else {
00191 file.delete();
00192 }
00193 }
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 public static final void copyFile(String sourcePath, String destPath) throws FileNotFoundException, IOException {
00215
00216 int index = destPath.lastIndexOf(File.separatorChar);
00217 if (index != -1) {
00218 String dirPath = destPath.substring(0, index);
00219 if (dirPath.length() !=0) {
00220 File dirfile = new File(dirPath);
00221 if (!dirfile.exists()) {
00222 dirfile.mkdirs();
00223 }
00224 }
00225 }
00226
00227 InputStream sourceFile = new BufferedInputStream(new FileInputStream(sourcePath));
00228 try {
00229 OutputStream destFile = new BufferedOutputStream(new FileOutputStream(destPath));
00230 try {
00231 byte[] buffer = new byte[1024];
00232 int bytes_read;
00233 while(( bytes_read = sourceFile.read(buffer)) != -1)
00234 destFile.write(buffer, 0, bytes_read);
00235 } finally {
00236 destFile.flush();
00237 destFile.close();
00238 }
00239 } finally {
00240 sourceFile.close();
00241 }
00242 }
00243
00244 }