java 实现对文件夹排序,找出最大的文件夹,


public class SortUtil
{
    
    /**
     * insertionSort;
     * @param data,    array[];
     * @param first,   0;
     * @param last,    array.length - 1;
     */
    public static void insertionSort(Comparable[] data, int first, int last)
    {
        for(int i = first + 1; i <= last; i++)
        {
            Comparable temp = data[i];
            int j = i - 1;
            for(; j >= first && data[j].compareTo(temp) > 0; j--)
            {
                data[j + 1] = data[j];
            }
            data[j + 1] = temp;
        }
    }
    
    /**
     * getMaxCompare;
     * @param data,   array[];
     * @return        Max Value;
     */
    public static Comparable getMaxCompare(Comparable[] data)
    {
        insertionSort(data, 0, data.length - 1);
        return data[data.length - 1];
    }
    
    public static void main(String[] args)
    {
        Comparable[] data = {3,2};//,4,12,43,654,12,242,124,151,87,1,7,1,3,2,9,3,6,2,7,8};
        insertionSort(data, 0 , data.length - 1);
        for(int i = 0; i < data.length; i++)
        {
            System.out.print(data[i] +",");
        }
        System.out.println();
        System.out.print(getMaxCompare(data));
    }

}







public class FileEntity implements Comparable
{
    /**
     * 5.01.01.T05$12.11.20T28,12.11.20T28$12.11.20T45
     */
    public static final String FileSeparatorTag = "T";
    public String filename = "5.01.01.T05$12.11.20"+FileSeparatorTag+"28";
    
    @Override
    public int compareTo(Object obj)
    {
        String nextFileName = ((FileEntity) obj).filename;
        return Integer.parseInt(this.filename.substring(this.filename.lastIndexOf(FileSeparatorTag) + 1)) - Integer.parseInt(nextFileName.substring(nextFileName.lastIndexOf(FileSeparatorTag) + 1));
    }

    public static void main(String[] args)
    {
        String tmp = "5.01.01.T05$12.11.20"+FileSeparatorTag+"28";;
        
        String  tmps = tmp.substring(tmp.lastIndexOf(FileSeparatorTag) + 1);
        System.out.println("" + tmps);
    }
  

}






public class FileClient
{

    /**
     * eg.dirs: 5.01.01.T05$12.11.20T28,12.11.20T28$12.11.20T45 getDirList
     * 
     * @param filepath ,directory ;
     * @return ,list directories;
     */
    public static FileEntity[] getListDirs(String filepath)
    {
        List tmpList = new ArrayList();
        File dir = new File(filepath);
        File[] files = dir.listFiles();
        for(int i = 0; i < files.length; i++)
        {
            String strFile = files[i].getAbsolutePath().substring(files[i].getAbsolutePath().lastIndexOf(File.separator) + 1);
            System.out.println(strFile.indexOf("$") + "," + strFile);
            if(files[i].isDirectory() && strFile.indexOf("$") > 0 && strFile.lastIndexOf("$") < strFile.length() - 1
                            && strFile.lastIndexOf(FileEntity.FileSeparatorTag) < strFile.length() - 1)
            {
                FileEntity entity = new FileEntity();
                entity.filename = files[i].getAbsolutePath();
                tmpList.add(entity);
            }
        }
        if(tmpList.size() > 0)
        {
            FileEntity[] dirs = new FileEntity[tmpList.size()];
            for(int i = 0; i < tmpList.size(); i++)
            {
                dirs[i] = (FileEntity)tmpList.get(i);
            }
            return dirs;
        }
        else
        {
            FileEntity entity = new FileEntity();
            entity.filename = filepath;
            return new FileEntity[]{entity};
        }
    }

    /**
     * getMaxDirAbsolutePath
     * 
     * @param filepath , input dir, directory;
     * @return , maxDirAbsolutePath;
     */
    public static String getMaxDirAbsolutePath(String filepath)
    {
        FileEntity[] dirs = getListDirs(filepath);
        for(int i = 0; i < dirs.length; i++)
        {
            System.out.println("dirs:" + dirs[i].filename);
        }

        SortUtil.insertionSort(dirs, 0, dirs.length - 1);
        for(int i = 0; i < dirs.length; i++)
        {
            System.out.println("insertionSort:" + dirs[i].filename);
        }

        FileEntity maxFileEntity = (FileEntity)SortUtil.getMaxCompare(dirs);
        String theMaxDir = maxFileEntity.filename;
        System.out.println("theMaxDir:" + theMaxDir);
        return theMaxDir;
    }

    /**
     * writeProperties
     * 
     * @param propFileName , properties file path;
     * @param key , key;
     * @param value , value;
     */

    public static void writeProperties(String propFileName, String key, String value)
    {
        File propFile = new File(propFileName);
        try
        {
            propFile.createNewFile();
            Properties prop = new Properties();
            // InputStream fis = new FileInputStream(propFileName);
            // // 从输入流中读取属性列表(键和元素对)
            // prop.load(fis);
            // 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
            // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
            OutputStream fos = new FileOutputStream(propFileName);
            prop.put(key, value);// .setProperty(key, value);
            // 以适合使用 load 方法加载到 Properties表中的格式,
            // 将此 Properties 表中的属性列表(键和元素对)写入输出流
            prop.store(fos, "Update '" + key + "' -->" + value);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * test_readProperties
     * 
     * @param propFileName
     * @param key
     * @param defaultValue
     * @return
     */
    public static String test_readProperties(String propFileName, String key, String defaultValue)
    {
        String value = defaultValue;
        try
        {
            InputStream fis = new FileInputStream(propFileName);
            Properties prop = new Properties();
            prop.load(fis);
            value = prop.getProperty(key, defaultValue);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        System.out.println("value:" + value);
        return value;
    }

    /**
     * eg.dirs: 5.01.01.T05$12.11.20T28,12.11.20T28$12.11.20T45
     * 
     * @param args
     */
    public static void main(String[] args)
    {
        String srcDirPath = "C:/scripts/";
        String destPropPath = "./";
        if(args.length == 2)
        {
            srcDirPath = args[0];
            destPropPath = args[1];
        }
        destPropPath = destPropPath + "theMaxDir" + ".properties";
        String theMaxDir = getMaxDirAbsolutePath(srcDirPath).replaceAll("\\\\", "\\/");
        System.out.println("theMaxDir:" + theMaxDir);

        System.out.println("user.dir=" + System.getProperty("user.dir"));

        writeProperties(destPropPath, "theMaxDir", theMaxDir);

        test_readProperties(destPropPath, "theMaxDir", srcDirPath);
    }

}


你可能感兴趣的:(java)