UUID与System.currentTimeMillis()产生一个新文件名的工具类

1.FileUtils.java

package Utils.GenerateNewFileName;

import java.util.UUID;

public class FileUtils {
    /**
         * 获取文件的新名称
         * @param fileName 文件名
         * @return 文件新生成的名称
         */
        public static String getNewFileName(String fileName) {
            StringBuffer newFileName = new StringBuffer();
            String extension = fileName.substring(fileName.lastIndexOf('.'));
            newFileName.append(UUID.randomUUID().toString());
            newFileName.append(extension);
            return newFileName.toString();
        }
        
        public static String getNewFileNameWithTimeStamp(String fileName) {
            StringBuffer newFileName = new StringBuffer();
            String extension = fileName.substring(fileName.lastIndexOf('.'));
            newFileName.append(System.currentTimeMillis());
            newFileName.append(extension);
            return newFileName.toString();
        }
}

 

 2.测试:

package Utils.GenerateNewFileName;

import org.junit.Test;

public class TestUtils {

    @Test
    public void Test1(){
        System.out.println(FileUtils.getNewFileName("1.DOC"));
        System.out.println(FileUtils.getNewFileName("1.X.DOCX"));
        System.out.println(FileUtils.getNewFileNameWithTimeStamp("1.X.DOCX"));
        System.out.println(FileUtils.getNewFileNameWithTimeStamp("1.DOCX"));
    }
}

 

 

 结果:

e02fd6b0-8e05-4762-a333-6d71ba2e0cb2.DOC
f703af3a-4213-41dd-a6e7-217c59045879.DOCX
1505551091376.DOCX
1505551091376.DOCX

 



 

posted @ 2017-09-16 11:48  QiaoZhi  阅读(640)  评论(0编辑  收藏  举报