Random names used in the project, file extensions, circular creation of directories

These are simple methods, record

Random name

/**
 * 生成随机文件名,当前年月日小时分钟秒钟 +五位随机数
 * 
 * @return
 */
public class RandomName {
    
    

	private static final SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
	private static final Random r = new Random();
	
	public static void main(String[] args) {
    
    
		
		// 获取随机的五位数
		int rannum = r.nextInt(89999) + 10000;
		String nowTimeStr = sDateFormat.format(new Date());
		System.out.println(nowTimeStr + rannum);
		// 输出   202102061615298392	
	}
	
}


File extension

/**
 * 获取输入文件流的扩展名
 * 
 * @return
 */
public class FileExtension{
    
    

	public static void main(String[] args) {
    
    
		String fileName = "认真的雪.mp4";
		System.out.println(fileName.substring(fileName.lastIndexOf(".")).replace(".", ""));
		// mp4
	}
}

Create a directory

/**
 * 创建目标路径所涉及到的目录,即/testA/testB/testC/A.jpg, 那么 testA testB testC
 * 这三个文件夹都得自动创建
 * 
 */
public class makeDirPath{
    
    

	
	public static void main(String[] args) {
    
    
		
		File dirPath = new File("D:/image/testA/testB/testC");
		if (!dirPath.exists()) {
    
    
			dirPath.mkdirs();
		}
	}
	
}

Guess you like

Origin blog.csdn.net/hyx1249273846/article/details/113727239