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
00066 public static String getPathFromCompleteFileName(String completeName) {
00067 int index = completeName.lastIndexOf(File.separatorChar);
00068 if (index != -1) {
00069 return completeName.substring(0, index+1);
00070 } else {
00071 return emptyString;
00072 }
00073 }
00074
00081 public static String getNameFromCompleteFileName(String completeName) {
00082 int index = completeName.lastIndexOf(File.separatorChar);
00083 if (index == -1) {
00084 return completeName;
00085 } else {
00086 if (index != completeName.length()) {
00087 return completeName.substring(index+1, completeName.length());
00088 }
00089 }
00090 return emptyString;
00091 }
00092
00096 public static void moveFile(String oldFileName, String newFileName) {
00097 File newFile = new File(newFileName);
00098
00099 if (newFile.exists()) {
00100 newFile.delete();
00101 }
00102 File oldFile = new File(oldFileName);
00103 oldFile.renameTo(newFile);
00104 if (oldFile.exists()) {
00105 oldFile.delete();
00106 }
00107 }
00108
00109 public static String concertToUnixFomat(String filePath) {
00110 return filePath.replace('\\', '/');
00111 }
00112
00113 public static byte[] readFile(String completeFileName) throws FileNotFoundException, IOException {
00114 FileInputStream fileStream = new FileInputStream(completeFileName);
00115 try {
00116
00117 byte[] buffer = new byte[1024];
00118 ByteArrayOutputStream stream = new ByteArrayOutputStream(buffer.length);
00119 int bytes_read;
00120 try {
00121 while(( bytes_read = fileStream.read(buffer)) != -1)
00122 stream.write(buffer, 0, bytes_read);
00123 } finally {
00124 stream.flush();
00125 stream.close();
00126 }
00127 return stream.toByteArray();
00128 } finally {
00129 fileStream.close();
00130 }
00131 }
00132
00133 public static byte[] readFile(File file) throws FileNotFoundException, IOException {
00134 FileInputStream fileStream = new FileInputStream(file);
00135 try {
00136
00137 byte[] buffer = new byte[1024];
00138 ByteArrayOutputStream stream = new ByteArrayOutputStream(buffer.length);
00139 int bytes_read;
00140 try {
00141 while(( bytes_read = fileStream.read(buffer)) != -1)
00142 stream.write(buffer, 0, bytes_read);
00143 } finally {
00144 stream.flush();
00145 stream.close();
00146 }
00147 return stream.toByteArray();
00148 } finally {
00149 fileStream.close();
00150 }
00151 }
00152
00158 public static final void removeAllFile(File file, String ext, boolean recurcif) {
00159 if ((recurcif) &&(file.isDirectory())) {
00160 File[] fileList = file.listFiles();
00161 for (int i=0; i<fileList.length; i++) {
00162 removeAllFile(fileList[i], ext, recurcif);
00163 }
00164 }
00165 if (ext != null) {
00166 if (file.getName().indexOf(ext) != -1) {
00167 file.delete();
00168 }
00169 } else {
00170 file.delete();
00171 }
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 public static final void copyFile(String sourcePath, String destPath) throws FileNotFoundException, IOException {
00194
00195 int index = destPath.lastIndexOf(File.separatorChar);
00196 if (index != -1) {
00197 String dirPath = destPath.substring(0, index);
00198 if (dirPath.length() !=0) {
00199 File dirfile = new File(dirPath);
00200 if (!dirfile.exists()) {
00201 dirfile.mkdirs();
00202 }
00203 }
00204 }
00205
00206 InputStream sourceFile = new BufferedInputStream(new FileInputStream(sourcePath));
00207 try {
00208 OutputStream destFile = new BufferedOutputStream(new FileOutputStream(destPath));
00209 try {
00210 byte[] buffer = new byte[1024];
00211 int bytes_read;
00212 while(( bytes_read = sourceFile.read(buffer)) != -1)
00213 destFile.write(buffer, 0, bytes_read);
00214 } finally {
00215 destFile.flush();
00216 destFile.close();
00217 }
00218 } finally {
00219 sourceFile.close();
00220 }
00221 }
00222
00223 }