FileUtil.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package kr.co.udapsoft.common.util;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.util.StringTokenizer;
  8. public class FileUtil
  9. {
  10. /**
  11. * Ư�� Dirctory ���� ���� check
  12. * ���� Directory ���� ��� ��
  13. * @param Path ���
  14. * @return String path�� �ٽ� return �Ѵ�.. <br>
  15. * /www//kkk/dd.html -> /www/kkk/dd.html ���·� return
  16. */
  17. public static String chkDirectory(String fname) throws Exception {
  18. String seperator = "";
  19. if( fname.indexOf("/") < 0 ) {
  20. // unix type
  21. seperator = "\\";
  22. }
  23. else {
  24. // windows type
  25. seperator = "/";
  26. }
  27. StringTokenizer st = new StringTokenizer(fname, seperator);
  28. String tVar = "";
  29. int stCnt = st.countTokens();
  30. // unix �迭���
  31. if ((fname.indexOf(":") < 0) && (fname.startsWith("/")))
  32. tVar = "/";
  33. for (int i = 0; i < stCnt; i++) {
  34. if (i != 0)
  35. tVar += seperator;
  36. tVar += st.nextToken();
  37. // Ȯ���ڰ� ������ return true
  38. if (tVar.indexOf(".") != -1) return tVar;
  39. // window���
  40. if ((i == 0) && ( tVar.indexOf(":") > -1))
  41. continue;
  42. if (!mkDir(tVar)) return null;
  43. }
  44. return fname;
  45. }
  46. /**
  47. * Ư�� ��ο� �����ϴ� FileList�� ������
  48. *
  49. * @param Path ���
  50. */
  51. public static String[] getFileList(String path) throws Exception {
  52. String[] FILE_LIST = null;
  53. File dir = new File(path);
  54. if (dir.isDirectory())
  55. FILE_LIST = dir.list();
  56. return FILE_LIST;
  57. }
  58. /**
  59. * File ���� üũ
  60. *
  61. * @param Path ���
  62. */
  63. public static boolean isFile(String path) throws Exception {
  64. File tFile = new File(path);
  65. return tFile.isFile() ;
  66. }
  67. /**
  68. * Directory ���� üũ
  69. *
  70. * @param Path ���
  71. */
  72. public static boolean isDir(String path) throws Exception {
  73. File dir = new File(path);
  74. if( dir.exists() && dir.isDirectory() )
  75. return true;
  76. return false;
  77. }
  78. /**
  79. * Directory ��
  80. *
  81. * @param Path ���
  82. */
  83. public static boolean mkDir(String path) throws Exception {
  84. if (path.length() < 1)
  85. return true;
  86. File dir = new File(path);
  87. if (dir.exists() && dir.isDirectory())
  88. return true;
  89. else
  90. return dir.mkdir();
  91. }
  92. /**
  93. * file delete
  94. *
  95. * @param Path ���
  96. */
  97. public static boolean delFile(String path) throws Exception {
  98. File tFile = new File(path);
  99. if (tFile.exists() && tFile.isFile())
  100. return tFile.delete();
  101. else
  102. return false;
  103. }
  104. /**
  105. * file delete
  106. *
  107. * @param Path ���
  108. */
  109. public static boolean delDir(String path) throws Exception {
  110. File tFile = new File(path);
  111. if (tFile.isDirectory())
  112. {
  113. String[] fileNames = tFile.list();
  114. for (int i = 0; i < fileNames.length; i++)
  115. {
  116. if (fileNames[i] == null || fileNames[i].equals(""))
  117. continue;
  118. File f = new File(path + File.separator + fileNames[i]);
  119. if (f.exists() && f.isFile())
  120. {
  121. f.delete();
  122. }
  123. else if (f.exists() && f.isDirectory())
  124. {
  125. delDir(path + File.separator + fileNames[i]);
  126. }
  127. }
  128. return tFile.delete();
  129. }
  130. else
  131. {
  132. return false;
  133. }
  134. }
  135. public static byte[] getBytesFromFile(String path) throws IOException {
  136. File file = new File(path);
  137. InputStream is = new FileInputStream(file);
  138. // Get the size of the file
  139. long length = file.length();
  140. // Create the byte array to hold the data
  141. byte[] bytes = new byte[(int)length];
  142. // Read in the bytes
  143. int offset = 0;
  144. int numRead = 0;
  145. while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0)
  146. {
  147. offset += numRead;
  148. }
  149. // Ensure all the bytes have been read in
  150. if (offset < bytes.length)
  151. {
  152. throw new IOException("Could not completely read file " + file.getName());
  153. }
  154. // Close the input stream and return bytes
  155. is.close();
  156. return bytes;
  157. }
  158. public static String getExtension(String filename) throws Exception
  159. {
  160. String extension = "";
  161. int k = filename.indexOf(".");
  162. if( k > -1 )
  163. extension = filename.substring(k + 1, filename.length());
  164. return extension ;
  165. }
  166. /**
  167. * Method copyFile.
  168. * @param argSource
  169. * @param argTarget
  170. * @return boolean
  171. * @throws Exception
  172. */
  173. public static boolean copyFile(String argSource, String argTarget) throws Exception
  174. {
  175. FileInputStream fis = null;
  176. FileOutputStream fos = null;
  177. byte[] temp = new byte[4096];
  178. try {
  179. fis = new FileInputStream(argSource);
  180. if( fis == null )
  181. throw new Exception("argSource or FileInputStream is null : " + argSource);
  182. fos = new FileOutputStream(argTarget);
  183. int offset = 0;
  184. int len = 0;
  185. while( (len = fis.read(temp) ) != -1 ) {
  186. fos.write(temp, 0, len);
  187. offset += len;
  188. }
  189. return true;
  190. }
  191. finally
  192. {
  193. if( fis != null ) fis.close();
  194. if( fos != null ) fos.close();
  195. }
  196. }
  197. /**
  198. * Method rename.
  199. * @param src
  200. * @param target
  201. * @return boolean
  202. * @throws Exception
  203. */
  204. public static boolean rename(String src, String target) throws Exception
  205. {
  206. return (new File(src)).renameTo(new File(target));
  207. }
  208. }