Reputation: 339
In Unix created a folder path like /data/test/files/2015/05/19
. From year, folder is autogenerated /data/test/files/$(date +%Y)/$(date +%m)/$(date +%d)
So .txt
file inside the above folder location should be parsed through Java code and moved to DB.
I tried to parse that location as 2015/05/19
will change in future, so tried to append current year/month/day in Java and then parse that particular file.
//To get current year
String thisYear = new SimpleDateFormat("yyyy").format(new Date());
System.out.println("thisYear :"+thisYear);
//To get current month
String thisMonth = new SimpleDateFormat("MM").format(new Date());
System.out.println("thisMonth : "+thisMonth);
//To get current date
String thisDay = new SimpleDateFormat("dd").format(new Date());
System.out.println("thisDay : "+thisDay);
File f = new File("\\data\\test\\files\\+"thisYear"+\\+"thisMonth"+\\+"thisDay"+ \\refile.txt");
The above didn't work so how can I use thisYear,thisMonth,thisDay
in path?
Upvotes: 3
Views: 675
Reputation: 16050
You could use the SimpleDateFormat class like:
String format = "yyyy/MM/dd".replace( "/" , File.separator );
SimpleDateFormat sdf = new SimpleDateFormat( format );
String pathPrefix = "/data/test/files/".replace( "/" , File.separator );
File f = new File( pathPrefix + sdf.format( new Date() ) + File.separator + "refile.txt" );
Remember that the path separator character is filesystem dependent, thus the File.separator
usage. You might also replace the new Date()
with something else, if you're not after "todays" directory.
If you need to scan through all date-formatted directories, that's another matter for another question, but do look at the File.list()
methods for that.
Cheers,
Upvotes: 1
Reputation: 297
Try this. I think this works.
File f = new File("/data/test/files/" + thisYear+ "/" + thisMonth+ "/" +thisDay + "/refile.txt");
Upvotes: 2