Java better way to delete file if exists

Starting from Java 7 you can use deleteIfExists that returns a boolean (or throw an Exception) depending on whether a file was deleted or not. This method may not be atomic with respect to other file system operations. Moreover if a file is in use by JVM/other program then on some operating system it will not be … Read more

Syntax error on token “;”, { expected after this token in Random string creator

In Java, you cannot directly write the executable statements in class. You need to move your code in a method. Only variables declaration is allowed outside the method/blocks. Just for the sake of testing, ,move everthing to main method. Try this: Note: system.out.println(arrayList); will throw an error because there is no varaible called arrayList, i think it should … Read more

Sort ArrayList of custom Objects by property

Since Date implements Comparable, it has a compareTo method just like String does. So your custom Comparator could look like this: The compare() method must return an int, so you couldn’t directly return a boolean like you were planning to anyway. Your sorting code would be just about like you wrote: A slightly shorter way to write all this, if you don’t need to reuse your comparator, is to … Read more

Coding Conventions – Naming Enums

Enums are classes and should follow the conventions for classes. Instances of an enum are constants and should follow the conventions for constants. So There is no reason for writing FruitEnum any more than FruitClass. You are just wasting four (or five) characters that add no information. This approach is recommended by and used in … Read more