123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- package kr.co.udapsoft.common.util;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.StringTokenizer;
- public class FileUtil
- {
- /**
- * Ư�� Dirctory ���� ���� check
- * ���� Directory ���� ��� ��
- * @param Path ���
- * @return String path�� �ٽ� return �Ѵ�.. <br>
- * /www//kkk/dd.html -> /www/kkk/dd.html ���·� return
- */
- public static String chkDirectory(String fname) throws Exception {
- String seperator = "";
-
- if( fname.indexOf("/") < 0 ) {
- // unix type
- seperator = "\\";
- }
- else {
- // windows type
- seperator = "/";
- }
-
- StringTokenizer st = new StringTokenizer(fname, seperator);
- String tVar = "";
- int stCnt = st.countTokens();
-
- // unix �迭���
- if ((fname.indexOf(":") < 0) && (fname.startsWith("/")))
- tVar = "/";
-
- for (int i = 0; i < stCnt; i++) {
- if (i != 0)
- tVar += seperator;
-
- tVar += st.nextToken();
- // Ȯ���ڰ� ������ return true
- if (tVar.indexOf(".") != -1) return tVar;
-
- // window���
- if ((i == 0) && ( tVar.indexOf(":") > -1))
- continue;
- if (!mkDir(tVar)) return null;
- }
-
- return fname;
- }
- /**
- * Ư�� ��ο� �����ϴ� FileList�� ������
- *
- * @param Path ���
- */
- public static String[] getFileList(String path) throws Exception {
- String[] FILE_LIST = null;
- File dir = new File(path);
- if (dir.isDirectory())
- FILE_LIST = dir.list();
- return FILE_LIST;
- }
- /**
- * File ���� üũ
- *
- * @param Path ���
- */
- public static boolean isFile(String path) throws Exception {
- File tFile = new File(path);
- return tFile.isFile() ;
- }
- /**
- * Directory ���� üũ
- *
- * @param Path ���
- */
- public static boolean isDir(String path) throws Exception {
- File dir = new File(path);
- if( dir.exists() && dir.isDirectory() )
- return true;
- return false;
- }
- /**
- * Directory ��
- *
- * @param Path ���
- */
- public static boolean mkDir(String path) throws Exception {
- if (path.length() < 1)
- return true;
- File dir = new File(path);
- if (dir.exists() && dir.isDirectory())
- return true;
- else
- return dir.mkdir();
- }
- /**
- * file delete
- *
- * @param Path ���
- */
- public static boolean delFile(String path) throws Exception {
- File tFile = new File(path);
- if (tFile.exists() && tFile.isFile())
- return tFile.delete();
- else
- return false;
- }
- /**
- * file delete
- *
- * @param Path ���
- */
- public static boolean delDir(String path) throws Exception {
- File tFile = new File(path);
-
- if (tFile.isDirectory())
- {
- String[] fileNames = tFile.list();
- for (int i = 0; i < fileNames.length; i++)
- {
- if (fileNames[i] == null || fileNames[i].equals(""))
- continue;
- File f = new File(path + File.separator + fileNames[i]);
- if (f.exists() && f.isFile())
- {
- f.delete();
- }
- else if (f.exists() && f.isDirectory())
- {
- delDir(path + File.separator + fileNames[i]);
- }
- }
- return tFile.delete();
- }
- else
- {
- return false;
- }
- }
- public static byte[] getBytesFromFile(String path) throws IOException {
- File file = new File(path);
-
- InputStream is = new FileInputStream(file);
-
- // Get the size of the file
- long length = file.length();
-
- // Create the byte array to hold the data
- byte[] bytes = new byte[(int)length];
-
- // Read in the bytes
- int offset = 0;
- int numRead = 0;
- while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0)
- {
- offset += numRead;
- }
-
- // Ensure all the bytes have been read in
- if (offset < bytes.length)
- {
- throw new IOException("Could not completely read file " + file.getName());
- }
-
- // Close the input stream and return bytes
- is.close();
-
- return bytes;
- }
- public static String getExtension(String filename) throws Exception
- {
- String extension = "";
- int k = filename.indexOf(".");
- if( k > -1 )
- extension = filename.substring(k + 1, filename.length());
- return extension ;
- }
- /**
- * Method copyFile.
- * @param argSource
- * @param argTarget
- * @return boolean
- * @throws Exception
- */
- public static boolean copyFile(String argSource, String argTarget) throws Exception
- {
- FileInputStream fis = null;
- FileOutputStream fos = null;
- byte[] temp = new byte[4096];
- try {
- fis = new FileInputStream(argSource);
- if( fis == null )
- throw new Exception("argSource or FileInputStream is null : " + argSource);
- fos = new FileOutputStream(argTarget);
- int offset = 0;
- int len = 0;
- while( (len = fis.read(temp) ) != -1 ) {
- fos.write(temp, 0, len);
- offset += len;
- }
- return true;
- }
- finally
- {
- if( fis != null ) fis.close();
- if( fos != null ) fos.close();
- }
- }
- /**
- * Method rename.
- * @param src
- * @param target
- * @return boolean
- * @throws Exception
- */
- public static boolean rename(String src, String target) throws Exception
- {
- return (new File(src)).renameTo(new File(target));
- }
- }
|