Optimizing
Your question is not clear about what you meant by optimizing.
Moving Lines Outside Loop
The lines of code that execute within the for loop need to run inside the loop. There is nothing unoptimized about that. I do not see anything in your code that can be moved outside the loop.
If you are worried about creating objects that will almost immediately be destroyed and become "garbage" for the JVM’s garbage collector… stop worrying. The modern JVMs are extremely well-honed and optimized for such short-lived objects. Don't create such objects frivolously in a long-running loop (hundreds of thousands or millions of iterations), but also don't fret about it.
In the same vein, see my code example below. Notice how I put the time zone, locale, and formatter before the loop. Those lines would work just as well inside the for loop, but they would be executing repeatedly and creating new instances with no benefit. Those three particular objects are built to be reused, so I moved them up top. When drafting code I might well have them in the loop, and later identify them as qualified for being moved out of the loop.
Fewer Lines
If by optimizing you meant reducing the number of lines for the sake of shorter code, I strongly recommend against that for three reasons.
- Readability of code is much more important than line count. Programmers need to be able easily read and make sense of code rather than decipher it.
- Debugging is much easier if you have simple short lines of code. By "short" I mean focused on one (or few) specific operation being accomplished (not actual line length). If too many nested operations occur within a line, then tracking values and effects when debugging becomes more complicated.
- The modern JVM technology such as Oracle’s HotSpot & JRockit do amazing things with optimizing code both during compilation and during runtime. Those optimizations are made most powerful when given relatively simple straightforward lines of code. Making code overly dense or "clever" can actual reduce opportunities for Java compiler/runtime optimization.
java.time
One big improvement you could make is to avoid the use of the java.util.Date/.Calendar & SimpleTextFormat classes. Those old classes are notoriously troublesome, flawed in both design and implementation.
Those classes have been supplanted in Java 8 and later by the new java.time package. This new date-time framework is inspired by the Joda-Time library, defined by JSR 310, and extended by the ThreeTen-Extra project.
That FileTime class you use is java.time-savvy. Note the toInstant method.
Assign the desired/expected time zone (ZoneId) to that Instant. If you do not specify a time zone, the JVM’s default time zone is implicitly applied – generally the better practice is to specify rather then rely implicitly on a default. Beware that the JVM’s current default time zone can be changed at runtime in any moment by any code in any thread of any app running within that JVM.
Create a String representation in a Locale-appropriate manner.
Let's imagine a Québécois manager looking at data pertaining to business operations in India. We want to show the date-time values formatted to her customs but framed in the time zone of India. Time zone is crucial in determining the date (a new day dawns earlier in India than in Canada).
final ZoneId zoneKolkata = ZoneId.of( "Asia/Kolkata" ); // Adjust the date-time value as it would appear in India. Or, ZoneId.systemDefault()
final Locale localeQuébécois = Locale.CANADA_FRENCH; // Or Locale.getDefault()
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate( FormatStyle.MEDIUM ).withLocale( localeQuébécois ).withZone( zoneKolkata );
final File startingFile = new File( "/Users/brainydeveloper/Downloads" ); // "G:\\TestFiles"
for ( File file : startingFile.listFiles( ) ) {
BasicFileAttributes attribute = null;
try {
attribute = Files.readAttributes( file.toPath( ), BasicFileAttributes.class );
} catch ( IOException e ) {
e.printStackTrace( ); // Add your own smart exception handling.
}
FileTime fileTime = attribute.creationTime( );
String output = formatter.format( fileTime.toInstant( ) );
System.out.println( output );
}
Example output.
2014-04-19
2010-10-12
2015-06-21
subDiranywhere within theforloop, so the whole loop seems redundant to me. Should it beBasicFileAttributes attribute = Files.readAttributes(subDir.toPath(), BasicFileAttributes.class);instead?forloop". Did you mean the BasicFileAttribute, FileTime, and String, lines? Why do you perceive the need to move them outside the loop? And what did you mean by "optimized in a much better way", less lines of code, better readability, better runtime performance (speed)?forloop, why not keep them outside and make use of them only once. Regarding optimizing, yes I mean speed and performance. Right now my directory ("G:\\TestFiles") has only 5 folders but if the number of folders are huge in the directory thenforloop will iterate those 3 lines every time it reads a folder. I hope I was able to explain my point.