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

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

随机名

/**
 * 生成随机文件名,当前年月日小时分钟秒钟 +五位随机数
 * 
 * @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();
		}
	}
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值