项目中用到的随机名,文件扩展名,循环创建目录

都是些简单的方法,记录下

随机名

/**
 * 生成随机文件名,当前年月日小时分钟秒钟 +五位随机数
 * 
 * @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	
	}
	
}


文件扩展名

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

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

创建目录

/**
 * 创建目标路径所涉及到的目录,即/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();
		}
	}
	
}
posted @ 2021-02-06 16:38  追风少年潇歌  阅读(73)  评论(0)    收藏  举报