欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

给文件改个名

程序员文章站 2022-07-15 10:18:59
...

最近下了个武林外传,可惜命名全是wulinwai60zhuan1ixxxxxx.mkv,wulinwai60zhuan2ixxxxxx.mkv这类的东西。一个一个改名那可是80集呢,懒得改了,于是乎准备写个程序来改一改。

 

最后发现写程序用的时间比一个一个改还慢。

 

 

 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;



public class FileRenamer
{

    private static Properties consProperties = new Properties();

    public static final String PATH_PREFIX = "resource\\";

    public static final String INPUT_FILE_NAME = "inputfile";

    public static String MODIFY_FILE_SUFFIX = "suffix";


    /**
     * this method get the properties object from the defination folder
     * 
     * 
     */

    public static void getProperties()
    {
        try
        {

            InputStreamReader isr = new InputStreamReader(new FileInputStream(
                new File(PATH_PREFIX + "file.properties")), "GBK");

            consProperties.load(isr);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
        catch (Exception t)
        {
            t.printStackTrace();
        }
    }


    /**
     * this method read the constant of the file which include the name of every
     * chapter remember you should transform the encoding of the stream to GBK ,
     * or you will get the messy code
     * 
     * @param file
     * @return
     */
    public static List readFileConstant(File file)
    {
        List tempList = new ArrayList();

        BufferedReader input = null;

        try
        {
            InputStreamReader isr = new InputStreamReader(new FileInputStream(
                file), "GBK");

            input = new BufferedReader(isr);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }

        String tempString = "";

        try
        {
            while (null != (tempString = input.readLine()))
            {
                tempList.add(tempString);
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return tempList;
    }


    /**
     * this method used for create some test file to read and rename please make
     * the name a little complex or it's easy to read and rename
     * 
     * @param filename
     * @param suffix
     */
    public static void createFile(String filename, String suffix)
    {
        try
        {
            FileOutputStream fos = new FileOutputStream(new File("resource\\"
                    + filename + suffix));
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }


    public static String[] filtrateFileName(String path, final String suffix)
    {
        File f = new File(path);

        String[] nameList = null;

        nameList = f.list(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name)
            {
                return name.endsWith(suffix);
            }
        });

        return nameList;
    }


    /**
     * this method used for sort the reading file name order the default order
     * like xx1xx.pdf , xx10xx.pdf....not xx1xx.pdf,xx2xx.pdf this is the core
     * technology of this app.
     * 
     * @param nameList
     */
    public static void sortFileName(String[] nameList)
    {
        List tempList = new ArrayList();

        for (int i = 0; i < nameList.length; i++)
        {
            tempList.add(nameList[i]);
        }

    }


    public static Map getNumberMap(String[] tempStringList,
        int NumberAppearTimes)
    {
        List setList = new ArrayList();

        for (int i = 0; i < NumberAppearTimes; i++)
        {
            setList.add(new LinkedHashSet());
        }

        String tempString = "";

        String recordNumString = "";

        char tempCharList[] = null;

        boolean mark = false;

        boolean numberRecorderFlag = false;

        int suffixLength = 0;

        int setCursor = 0;

        suffixLength = tempStringList[0].length()
                - tempStringList[0].lastIndexOf(".") + 1;

        for (int i = 0; i < tempStringList.length; i++)
        {
            tempString = tempStringList[i];

            tempCharList = tempString.toCharArray();

            for (int j = 0; j < tempCharList.length - suffixLength; j++)
            {
                if (tempCharList[j] >= 48 && tempCharList[j] <= 57)
                {
                    recordNumString += String.valueOf(tempCharList[j]);

                    mark = true;
                }
                else
                {
                    if (mark == true)
                    {
                        mark = false;

                        ((Set) setList.get(setCursor)).add((recordNumString));
                        setCursor++;
                        recordNumString = "";
                    }

                }

                if (j == tempCharList.length - (1 + suffixLength))
                {
                    if (tempCharList[j] > 48 && tempCharList[j] < 57)
                    {
                        setCursor++;
                        ((Set) setList.get(setCursor)).add((recordNumString));
                        recordNumString = "";
                    }
                }

            }

            setCursor = 0;
            recordNumString = "";

        }
        System.out.println("the size is : " + ((Set) setList.get(1)).size());

        Map tempMap = null;

        for (int i = 0; i < NumberAppearTimes; i++)
        {
            if (((Set) setList.get(i)).size() == tempStringList.length)
            {
                tempMap = new HashMap();

                Iterator it = ((Set) setList.get(i)).iterator();

                for (int j = 0; j < tempStringList.length; j++)
                {
                    String tempStr = "";

                    tempStr = it.next().toString();

                    tempMap.put(tempStr, tempStringList[j]);

                }

            }
        }

        return tempMap;

    }


    public static int getNumberTimes(String[] tempStringList)
    {

        int NumRecorder = 0;

        char tempCharList[] = null;

        boolean numberRecorderFlag = true;

        tempCharList = tempStringList[0].toCharArray();

        for (int j = 0; j < tempCharList.length; j++)
        {
            if (tempCharList[j] >= 48 && tempCharList[j] <= 57)
            {
                if (numberRecorderFlag == true)
                {
                    NumRecorder++;
                    numberRecorderFlag = false;
                }
            }
            else
            {
                numberRecorderFlag = true;
            }
        }

        return NumRecorder;
    }


    public static List sortList(List paramList)
    {
        String tempStr = "";

        for (int i = 0; i < paramList.size(); i++)
        {
            for (int j = i; j < paramList.size(); j++)
            {
                if (Integer.valueOf(paramList.get(i).toString()) > Integer
                    .valueOf(paramList.get(j).toString()))
                {
                    tempStr = (String) paramList.get(i);

                    paramList.set(i, paramList.get(j));

                    paramList.set(j, tempStr);
                }
            }
        }

        return paramList;

    }


    public static void main(String args[])
    {
        getProperties();

        List nameList = readFileConstant(new File(PATH_PREFIX
                + consProperties.getProperty(INPUT_FILE_NAME)));
        
        String suffix = consProperties.getProperty(MODIFY_FILE_SUFFIX);
        

        String[] testStringList = filtrateFileName(PATH_PREFIX, "pdf");
        
        Map nameMap = new HashMap();
        
        String tempFileName = "";

        nameMap = getNumberMap(testStringList,getNumberTimes(testStringList));
        
        for (int i = 0; i < 80; i++)
        {
            createFile("wulinwai11zhuan" + i + "xxxxxx", ".pdf");
        }
        
        for(int i = 0 ; i < nameList.size();i++)
        {
            File tempFile = new File(PATH_PREFIX+nameMap.get(String.valueOf(i)));
            
            File DestFile = new File(PATH_PREFIX+nameList.get(i)+suffix);
            
            if(tempFile.exists())
            {
                tempFile.renameTo(DestFile);
            }
        }

    }
}

 

相关标签: Java J# F#