⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 virtualdirectory.java

📁 一个利用Java语言实现的ftp程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        return "group";
    }
    
    
    /**
     * Get link count
     */
    private String getLinkCount(File fl) {
        if(fl.isDirectory()) {
            return String.valueOf(3);
        }
        else {
            return String.valueOf(1);
        }
    }
    
    
    /**
     * Get size
     */
    private String getLength(File fl) {
        String initStr = "            ";
        long sz = 0;
        if(fl.isFile()) {
            sz = fl.length();
        }
        String szStr = String.valueOf(sz);
        if(szStr.length() > initStr.length()) {
            return szStr;
        }
        return initStr.substring(0, initStr.length() - szStr.length()) + szStr;
    }
    
    
    /**
     * Get last modified date string.
     */
    private String getLastModified(File fl) {
        long modTime = fl.lastModified();
        Date date = new Date(modTime);
        return DateUtils.getUnixDate(date);
    }
    
    
    /**
     * Get file name.
     */
    private String getName(File fl) {
        return fl.getName();
    }
   
    
    /**
     * Get permission string.
     */
    private String getPermission(File fl) {

        StringBuffer sb = new StringBuffer(13);
        if(fl.isDirectory()) {
            sb.append('d');
        }
        else {
            sb.append('-');
        }
        
        if (fl.canRead()) {
            sb.append('r');
        }
        else {
            sb.append('-');
        }
        
        if (mbWritePerm && fl.canWrite()) {
            sb.append('w');
        }
        else {
            sb.append('-');
        }
        sb.append("-------");
        return sb.toString();
    }
    
    
    /**
     * Normalize separate characher. Separate character should be '/' always.
     */
    private String normalizeSeparateChar(String pathName) {   
       pathName = pathName.replace(File.separatorChar, '/');
       pathName = pathName.replace('\\', '/');
       return pathName;
    }
    
    
    /**
     * Replace dots. Returns physical name.
     * @param inArg the virtaul name
     */
    private String replaceDots(String inArg) {
        
        // get the starting directory
        String resArg;
        if(inArg.charAt(0) != '/') {
            resArg = mstRoot + mstCurrDir.substring(1);
        }
        else {
            resArg = mstRoot;
        }     
        
        // strip last '/'   
        if(resArg.charAt(resArg.length() -1) == '/') {
            resArg = resArg.substring(0, resArg.length()-1);
        }
        
        // replace ., ~ and ..        
        StringTokenizer st = new StringTokenizer(inArg, "/");
        while(st.hasMoreTokens()) {

            String tok = st.nextToken().trim();

            // . => current directory
            if(tok.equals(".")) {
                continue;
            }

            // .. => parent directory (if not root)
            if(tok.equals("..")) {
                if(resArg.startsWith(mstRoot)) {
                  int slashIndex = resArg.lastIndexOf('/');
                  if(slashIndex != -1) {
                    resArg = resArg.substring(0, slashIndex);
                  }
                }
                continue;
            }
            
            // ~ => home directory (in this case /)
            if (tok.equals("~")) {
                resArg = mstRoot.substring(0, mstRoot.length()-1).trim();
                continue;
            }
            
            resArg = resArg + '/' + tok;
        }
        
        // add last slash if necessary
        if( !inArg.equals("") && (inArg.charAt(inArg.length()-1)=='/') ) {
            resArg = resArg + '/';
        }
        
        // final check
        if (resArg.length() < mstRoot.length()) {
            resArg = mstRoot;
        }
        
        return resArg;
    }
    
    
    /**
     * Get each directory line.
     */
    private void printLine(File fl, Writer out) throws IOException {
        out.write(getPermission(fl));
        out.write(DELIM);
        out.write(DELIM);
        out.write(DELIM);
        out.write(getLinkCount(fl));
        out.write(DELIM);
        out.write(getOwner(fl));
        out.write(DELIM);
        out.write(getGroup(fl));
        out.write(DELIM);
        out.write(getLength(fl));
        out.write(DELIM);
        out.write(getLastModified(fl));
        out.write(DELIM);
        out.write(getName(fl));
    }
        
    /**
     * If the string is not '/', remove last slash.
     */
    private String removeLastSlash(String str) {
        if ( (str.length()>1) && (str.charAt(str.length()-1)=='/') ) {
            str = str.substring(0, str.length()-1);
        }
        return str;
    }
    
    
    
    //////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////
    /**
     * Inner class to compare files
     */
    private class FileComparator implements Comparator {
        public int compare(Object o1, Object o2) {
            String s1 = ((File)o1).getName();
            String s2 = ((File)o2).getName();
            return s1.compareTo(s2);
        }
    } 
        
        
    /////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////
    /**
     * File lister to list files properly
     */    
    private class FileLister {
        
        private File[] files = null;
        private boolean bAll = false;
        private boolean bDetail = false; 
        
        /**
         * Parse arguments - get options and get file list.
         */
        public FileLister(String argument) {
            String lsDirName = "./";
            String options = "";
            String pattern   = "*";
        
            // get options, directory name and pattern
            if(argument != null) {
                argument = argument.trim();
                StringBuffer optionsSb = new StringBuffer(4);
                StringTokenizer st = new StringTokenizer(argument, " ");
                while(st.hasMoreTokens()) {
                    String token = st.nextToken();
                    if(token.charAt(0) == '-') {
                        if (token.length() > 1) {
                            optionsSb.append(token.substring(1));
                        }
                    }
                    else {
                       lsDirName = token;
                    }
                }
                options = optionsSb.toString();
            }

            // check options
            bAll = options.indexOf('a') != -1;
            bDetail = options.indexOf('l') != -1;

            // check pattern
            lsDirName = getPhysicalName(lsDirName);
            File lstDirObj = new File(lsDirName);
            if ( !(lstDirObj.exists() && lstDirObj.isDirectory()) ) {
                int slashIndex = lsDirName.lastIndexOf('/');
                if( (slashIndex != -1) && (slashIndex != (lsDirName.length() -1)) ) {
                    pattern = lsDirName.substring(slashIndex+1);
                    lsDirName = lsDirName.substring(0, slashIndex+1);
                }
            }
        
            // check directory
            lstDirObj = new File(lsDirName);
            if(!lstDirObj.exists()) {
                return;
            }
            if(!lstDirObj.isDirectory()) {
                return;
            }

            // get file list
            if ( (pattern == null) || pattern.equals("*") || pattern.equals("") ) {
                files = lstDirObj.listFiles();
            }
            else {
                files = lstDirObj.listFiles(new FileRegularFilter(pattern));
            }
            //Arrays.sort(files, new FileComparator());
        }
        
        
        /**
         * Get files
         */
        public File[] getFiles() {
            return files;
        } 
        
        /**
         * Display all flag
         */
        public boolean isAll() {
            return bAll;
        } 
        
        /**
         * Display detail flag
         */
        public boolean isDetail() {
            return bDetail;
        } 
    }
        
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -