java新建文件路径使linux系统和windows系统都可用

 根据年月日创建文件夹,

windows系统路径例子:F:\year\month\day\fileName

linux系统路径例子:year/month/day/fileName

可以看出区别就在于斜杠与反斜杠,使用File.separator可以方便解决系统不兼容 

Calendar cal = Calendar.getInstance();
String month = (cal.get(Calendar.MONTH)+1)+"";  //当月
String year = (cal.get(Calendar.YEAR))+"";      //当年
String day = (cal.get(Calendar.DAY_OF_MONTH))+"";  //当日
String fileName = "CSDN.txt"; //文件名称
File newFile = new File("F:"+File.separator+year+File.separator+month+File.separator+day+File.separator+fileName);
newFile.mkdirs();//判断年 月 日文件夹是否创建,没有则新建

注: 关于File.separator详解

你可能感兴趣的:(开发技巧)