根据当前日期生成多层级目录,按年-日-月来生成。
接收文件需要根据日期接收,然后将每天接收的文件放在不同的文件夹下,好区分。
这是在d盘下生成的2019/5/2的多层级目录文件夹
方法
/**
* @description: TODO 默认创建多层日期 年-月-日 分层文件夹,默认创建到d盘下
* @param ${null}
* @return ${url路径}
* @throws
* @author
* @date 2019/4/30 21:16
*/
public static String getDatePathByCreateFile(){
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateInstance();
String time = dateFormat.format(date);
//System.out.println("获取到精确到日的时间格式为"+time);
String[] str = time.split("-");//根据‘-’进行拆分字符串 拆分出来的日期有,年,日,月,根据年月日创建文件夹
//System.out.println("当前年份为"+str[0]);
//System.out.println("当前月份为:"+str[1]);
//System.out.println("当前的日为:"+str[2]);
//System.out.println("------------------------文件写入-------------------------");
//创建文件夹
String filePath = "d:/"+str[0]+"/"+str[1]+"/"+str[2];
File file = new File(filePath);
boolean b = file.mkdirs();//这个方法的含义时即使抽象的父目录不存在,也会创建出不存在的抽象父目录
if(b){
System.out.println("不存在,已创建!");
}else{
System.out.println("存在,没有创建!");
}
//System.out.println("---------------------------文件写入结束---------------------------");
return filePath;
}
/**
* @description: TODO 根据参数dish也就是盘符创建进哪个文件夹
* @param ${dish 盘符, c盘就填c,d盘就填d}
* @return ${return_type}
* @throws
* @author
* @date 2019/4/30 21:27
*/
public static String getDatePathByCreateFile(String dish){
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateInstance();
String time = dateFormat.format(date);
//System.out.println("获取到精确到日的时间格式为"+time);
String[] str = time.split("-");//根据‘-’进行拆分字符串 拆分出来的日期有,年,日,月,根据年日月创建文件夹
//System.out.println("当前年份为"+str[0]);
//System.out.println("当前月份为:"+str[1]);
//System.out.println("当前的日为:"+str[2]);
//System.out.println("------------------------文件写入-------------------------");
//创建文件夹
String filePath = dish+":/"+str[0]+"/"+str[1]+"/"+str[2];
File file = new File(filePath);
if(file.exists()){
System.out.println("文件已存在");
}else {
boolean b = file.mkdirs();//这个方法的含义时即使抽象的父目录不存在,也会创建出不存在的抽象父目录
if (b) {
System.out.println("不存在,已创建!");
} else {
System.out.println("存在,不需要创建!");
}
//System.out.println("---------------------------文件写入结束---------------------------");
}
return filePath;
}
public static void main(String[] args) {
//在c盘下创建一个年月日多层级目录
String path = getDatePathByCreateFile("c");
System.out.println(path);
}