CscBrowser.java 9.7 KB
Newer Older
Naung1 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
package com.csc.library.utilities;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Vector;
public class CscBrowser {
    private File myFiles = null;
    private File[] myFilesArr = null;
    private int currentFile = -1;
    private int fileCount = 0;
    
    public CscBrowser() {
    	super();
    }
    public CscBrowser(String mfile) {
        if (mfile.trim().length() > 0) {
            this.setmyFiles(mfile);
            this.setmyFilesArr();
        }
    }
    public void setmyFiles(String strfile) {
        this.myFiles = new File(strfile);
    }
    /**
     *  method for set value to array that store file and directory list
     *  file address is array to store only address of file ([index][address])
     * */
    protected void setmyFilesArr() {
        this.myFilesArr = this.myFiles.listRoots();
        this.myFilesArr = this.myFiles.listFiles();
        this.setFileCount(this.fetchTotalFilesCount());
    }
    public File getmyFiles() {
        return this.myFiles;
    }
    public File[] getmyFilesArr() {
        return this.myFilesArr;
    }
    /**
     * method for set total of file count
     * */
    public int fetchTotalFilesCount() {
        File tmp = null;
        int count = 0;
        while (this.next()) {
            if(this.getAbsoluteFile().isFile()){
                count++;
            }           
        }
        this.setFileCount(count);
        this.reset();
        return count;
    }
    public void setOrderBy(File[] mfile) {
        Arrays.sort(mfile);
    }
    /**
     *  @param mtype  mode sort by 1=Filename, 2=Size, 3=Date, 4=Type
     * */
    public void setOrderBy(int mtype) {
        Arrays.sort(this.myFilesArr, new FileComp(mtype));
    }
    public String setDateFormatFile(Date mdate) {
        DateFormat dateFormat = DateFormat.getInstance();
        return dateFormat.format(mdate);
    }
    public String getFileFolder() {
        this.setOrderBy(this.myFilesArr);
        String date = "";
        for (int i = 0; i < this.myFilesArr.length; i++) {
            if (!this.myFilesArr[i].isDirectory()) {
                date = setDateFormatFile(new Date(this.myFilesArr[i].lastModified()));
                //System.out.println(this.myFilesArr[i].getName() + " Modify : " + date);
            } else {
                // System.out.println(this.myFilesArr[i].getName() + "[DIR]");
            }
        }
        return "";
    }
    public boolean isFolder() {
            if (!this.myFilesArr[currentFile].isDirectory()) {
            	return false;
            }
        return true;
    }    
    private boolean chkLastfile() {
        boolean chk = false;
        if (this.getmyFilesArr() != null) {
            if (this.currentFile < this.getmyFilesArr().length - 1) {
                return true;
            }
        }
        return false;
    }
    public boolean isLast() {       
        return !this.chkLastfile();
    }
    public boolean nextFile() {
        boolean nx = false;
        if (!this.chkLastfile())
            return nx;
        while (this.next()) {
            if (this.getAbsoluteFile().isFile())
                return true;
        }
        return nx;
    }
    public boolean next() {
        boolean nx = false;
        if (this.chkLastfile()) {
            this.currentFile++;
            nx = true;
        } else {
            nx = false;
        }
        return nx;
    }
    public void reset(){
        this.currentFile = -1;
    }
    public File getAbsoluteFile() {
        return this.myFilesArr[this.currentFile];
    }
    public File getAbsoluteFile(int index) {
        return this.myFilesArr[index];
    }
    public String getFileName() {
        return this.myFilesArr[this.currentFile].getName();
    }
    public boolean isAbsolute() {
        return this.myFilesArr[this.currentFile].isAbsolute();
    }
    public boolean isDirectory() {
        return this.myFilesArr[this.currentFile].isDirectory();
    }
    public boolean isHidden() {
        return this.myFilesArr[this.currentFile].isHidden();
    }
    public boolean isFile() {
        return this.myFilesArr[this.currentFile].isFile();
    }
    public String getMimeType() {
        String fName = this.myFilesArr[this.currentFile].getName();
        fName = fName.toLowerCase();
        if (fName.endsWith(".jpg") || fName.endsWith(".jpeg") || fName.endsWith(".jpe"))
            return "image/jpeg";
        else if (fName.endsWith(".gif"))
            return "image/gif";
        else if (fName.endsWith(".pdf"))
            return "application/pdf";
        else if (fName.endsWith(".htm") || fName.endsWith(".html") || fName.endsWith(".shtml"))
            return "text/html";
        else if (fName.endsWith(".avi"))
            return "video/x-msvideo";
        else if (fName.endsWith(".mov") || fName.endsWith(".qt"))
            return "video/quicktime";
        else if (fName.endsWith(".mpg") || fName.endsWith(".mpeg") || fName.endsWith(".mpe"))
            return "video/mpeg";
        else if (fName.endsWith(".zip"))
            return "application/zip";
        else if (fName.endsWith(".tiff") || fName.endsWith(".tif"))
            return "image/tiff";
        else if (fName.endsWith(".rtf"))
            return "application/rtf";
        else if (fName.endsWith(".mid") || fName.endsWith(".midi"))
            return "audio/x-midi";
        else if (fName.endsWith(".xl") || fName.endsWith(".xls") || fName.endsWith(".xlv") || fName.endsWith(".xla") || fName.endsWith(".xlb") || fName.endsWith(".xlt") || fName.endsWith(".xlm") || fName.endsWith(".xlk"))
            return "application/excel";
        else if (fName.endsWith(".doc") || fName.endsWith(".doc"))
            return "application/msword";
        else if (fName.endsWith(".png"))
            return "image/png";
        else if (fName.endsWith(".xml"))
            return "text/xml";
        else if (fName.endsWith(".svg"))
            return "image/svg+xml";
        else
            return "text/plain";
    }
    public String getFileSize() {
        long msize = 0;
        String filesize = "";
        try {
            File fsize = null;
            fsize = this.myFilesArr[this.currentFile].getCanonicalFile();
            msize = fsize.length();
            filesize = convertFileSize(msize);
        } catch (Exception ex) {
        	MyLog.error(this, ex);
        }
        return filesize;
    }
    public String getFileModify() {
        return setDateFormatFile(new Date(this.myFilesArr[this.currentFile].lastModified()));
    }
    static Vector expandFileList(String[] files, boolean inclDirs) {
        Vector v = new Vector();
        if (files == null)
            return v;
        for (int i = 0; i < files.length; i++)
            v.add(new File(URLDecoder.decode(files[i])));
        for (int i = 0; i < v.size(); i++) {
            File f = (File) v.get(i);
            if (f.isDirectory()) {
                File[] fs = f.listFiles();
                for (int n = 0; n < fs.length; n++)
                    v.add(fs[n]);
                if (!inclDirs) {
                    v.remove(i);
                    i--;
                }
            }
        }
        return v;
    }
    /**
     * Method to build an absolute path
     * @param dir the root dir
     * @param name the name of the new directory
     * @return if name is an absolute directory, returns name, else returns dir+name
     */
    static String getDir(String dir, String name) {
        if (!dir.endsWith(File.separator))
            dir = dir + File.separator;
        File mv = new File(name);
        String new_dir = null;
        if (!mv.isAbsolute()) {
            new_dir = dir + name;
        } else
            new_dir = name;
        return new_dir;
    }
    /**
     * Copies all data from in to out
     * 	@param in the input stream
     *	@param out the output stream
     *	@param buffer copy buffer
     */
    static void copyStreams(InputStream in, OutputStream out, byte[] buffer) throws IOException {
        copyStreamsWithoutClose(in, out, buffer);
        in.close();
        out.close();
    }
    /**
     * Copies all data from in to out
     * 	@param in the input stream
     *	@param out the output stream
     *	@param buffer copy buffer
     */
    static void copyStreamsWithoutClose(InputStream in, OutputStream out, byte[] buffer) throws IOException {
        int b;
        while ((b = in.read(buffer)) != -1)
            out.write(buffer, 0, b);
    }
    static String convertFileSize(long size) {
        int divisor = 1;
        String unit = "bytes";
        if (size >= 1024 * 1024) {
            divisor = 1024 * 1024;
            unit = "MB";
        } else if (size >= 1024) {
            divisor = 1024;
            unit = "KB";
        }
        if (divisor == 1)
            return size / divisor + " " + unit;
        String aftercomma = "" + 100 * (size % divisor) / divisor;
        if (aftercomma.length() == 1)
            aftercomma = "0" + aftercomma;
        return size / divisor + "." + aftercomma + " " + unit;
    }
    /**
     * Returns the fileCount.
     * @return int
     */
    public int getFileCount() {
        return fileCount;
    }
    /**
     * Sets the fileCount.
     * @param fileCount The fileCount to set
     */
    public void setFileCount(int fileCount) {
        this.fileCount = fileCount;
    }
    public static void main(String args[]) {
        CscBrowser cb = new CscBrowser("D:/EnterprisePT/JbossTOA/server/default/deploy/HRPEAP.ear/hrAppWeb.war/Payroll/upload/report/EXPORT/XML/PVF/");
        //cb.setOrderBy(1);
        File tmp = null;
        int i=0;
        while(cb.nextFile()){
            System.out.println("No."+(i++)+"    "+cb.isLast());
        }
    }
}