boolean existsFlag = (new File("C:/temp/temp.txt")).exists();
if (existsFlag) {
// File or directory exists
} else {
// File or directory does not exist
}
Saturday, June 27, 2009
How to Create a File
6:37 PM
Posted by
Techsavy
try {
File file = new File("C:/temp/temp.txt");
} catch (IOException e) {
}
File file = new File("C:/temp/temp.txt");
} catch (IOException e) {
}
How to Determine If a File Path Is a File or a Directory
6:34 PM
Posted by
Techsavy
File file = new File("filepath");
boolean isDirectory = file.isDirectory();
if (isDirectory) {
// file is a directory
} else {
// file is a file
}
boolean isDirectory = file.isDirectory();
if (isDirectory) {
// file is a directory
} else {
// file is a file
}
How to Get an Absolute File Path
6:30 PM
Posted by
Techsavy
File file = new File("C:"+File.separatorChar+"temp"+File.separatorChar+"temp.txt");
file = file.getAbsoluteFile(); // c:\temp\temp.txt
file = file.getAbsoluteFile(); // c:\temp\temp.txt
How to Construct a File Path
6:23 PM
Posted by
Techsavy
String path = File.separator +"temp"; // /temp
Thursday, June 25, 2009
How to Compare Dates
8:36 PM
Posted by
Techsavy
Calendar cal1 = new GregorianCalendar(2007, Calendar.SEPTEMBER, 1);
Calendar cal2 = new GregorianCalendar(2007, Calendar.SEPTEMBER, 15);
// Determine which is earlier
boolean b = cal1.after(cal2); // false
b = cal1.before(cal2); // true
// Get difference in milliseconds
long diffMillis = cal2.getTimeInMillis()-cal1.getTimeInMillis(); //1209600000
Calendar cal2 = new GregorianCalendar(2007, Calendar.SEPTEMBER, 15);
// Determine which is earlier
boolean b = cal1.after(cal2); // false
b = cal1.before(cal2); // true
// Get difference in milliseconds
long diffMillis = cal2.getTimeInMillis()-cal1.getTimeInMillis(); //1209600000
How to Create a Date Object
8:36 PM
Posted by
Techsavy
Calendar cal = new GregorianCalendar(2007, Calendar.SEPTEMBER, 1);
Date date = cal.getTime(); // Sat Sep 01 00:00:00 PDT 2007
Date date = cal.getTime(); // Sat Sep 01 00:00:00 PDT 2007
Subscribe to:
Posts (Atom)