try {
BufferedReader in = new BufferedReader(new FileReader("C:/temp/temp.txt"));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
} catch (IOException e) {
}
Saturday, June 27, 2009
How to Read Text from Standard Input
11:26 PM
Posted by
Techsavy
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = "";
while (str != null) {
str = in.readLine();
System.out.println(str);
}
} catch (IOException e) {
}
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = "";
while (str != null) {
str = in.readLine();
System.out.println(str);
}
} catch (IOException e) {
}
How to Delete a Directory
11:24 PM
Posted by
Techsavy
boolean success = (new File("directoryName")).delete();
if (success) {
// Deletion successful
}
if (success) {
// Deletion successful
}
How to Get the Current Working Directory
11:23 PM
Posted by
Techsavy
String currrentDirectory = System.getProperty("user.dir");
How to Rename a File
6:48 PM
Posted by
Techsavy
File oldFile = new File("C:/temp/oldTemp.txt");
File newFile = new File("C:/temp/newTemp.txt");
// Rename file
boolean success = oldFile.renameTo(newFile);
if (success) {
// File was successfully renamed
}
File newFile = new File("C:/temp/newTemp.txt");
// Rename file
boolean success = oldFile.renameTo(newFile);
if (success) {
// File was successfully renamed
}
How Delete a File
6:45 PM
Posted by
Techsavy
boolean success = (new File("C:/temp/temp.txt")).delete();
if (success) {
// Deletion succeeded
}
if (success) {
// Deletion succeeded
}
How to Get the Size of a File
6:42 PM
Posted by
Techsavy
File file = new File("C:/temp/temp.txt");
// Get the number of bytes in the file
long length = file.length();
// Get the number of bytes in the file
long length = file.length();
How to Determine If a File or Directory Exists
6:39 PM
Posted by
Techsavy
boolean existsFlag = (new File("C:/temp/temp.txt")).exists();
if (existsFlag) {
// File or directory exists
} else {
// File or directory does not exist
}
if (existsFlag) {
// File or directory exists
} else {
// File or directory does not exist
}
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
How to Get the Current Date
8:35 PM
Posted by
Techsavy
Calendar cal = new GregorianCalendar();
// Get the components of the date
int era = cal.get(Calendar.ERA); // 1
int year = cal.get(Calendar.YEAR); // 2009
int month = cal.get(Calendar.MONTH); // 0=Jan, 1=Feb, ...
int day = cal.get(Calendar.DAY_OF_MONTH); // 25
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); // 1=Sunday, 2=Monday, ...
// Get the components of the date
int era = cal.get(Calendar.ERA); // 1
int year = cal.get(Calendar.YEAR); // 2009
int month = cal.get(Calendar.MONTH); // 0=Jan, 1=Feb, ...
int day = cal.get(Calendar.DAY_OF_MONTH); // 25
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); // 1=Sunday, 2=Monday, ...
How to Get the Current Time in Another Time Zone
8:35 PM
Posted by
Techsavy
// Get the current time in London
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("London"));
int hour12 = cal.get(Calendar.HOUR);
int minutes = cal.get(Calendar.MINUTE);
int seconds = cal.get(Calendar.SECOND);
int am = cal.get(Calendar.AM_PM);
// Get the current hour-of-day at GMT
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
int hour24 = cal.get(Calendar.HOUR_OF_DAY);
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("London"));
int hour12 = cal.get(Calendar.HOUR);
int minutes = cal.get(Calendar.MINUTE);
int seconds = cal.get(Calendar.SECOND);
int am = cal.get(Calendar.AM_PM);
// Get the current hour-of-day at GMT
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
int hour24 = cal.get(Calendar.HOUR_OF_DAY);
How to Get the Current Time
8:34 PM
Posted by
Techsavy
Calendar cal = new GregorianCalendar();
// Get the components of the time
int hour12 = cal.get(Calendar.HOUR);
int hour24 = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
int millisecond = cal.get(Calendar.MILLISECOND);
int ampm = cal.get(Calendar.AM_PM);
// Get the components of the time
int hour12 = cal.get(Calendar.HOUR);
int hour24 = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
int millisecond = cal.get(Calendar.MILLISECOND);
int ampm = cal.get(Calendar.AM_PM);
How to Sort an Array
8:34 PM
Posted by
Techsavy
String[] strArray = new String[] {"x", "y", "Z"};
Arrays.sort(strArray);
// [Z, x, y]
Arrays.sort(strArray);
// [Z, x, y]
How to Use Sorted Map
8:33 PM
Posted by
Techsavy
Keys in the TreeMap will automatically be sorted.
Map map = new TreeMap();
map.put("a", new Integer(1));
map.put("c", new Integer(3));
map.put("b", new Integer(2));
// [a=1, b=2, c=3]
Map map = new TreeMap();
map.put("a", new Integer(1));
map.put("c", new Integer(3));
map.put("b", new Integer(2));
// [a=1, b=2, c=3]
How to Use Hash Table
8:32 PM
Posted by
Techsavy
Difference between HashMap and Hashtable :
1) HashMap accepts null key, whereas Hashtable doesn't.
2) HashMap is not thread-safe, whereas Hashtable is thread-safe.
Map map = new Hashtable();
map.put("a", new Integer(1));
map.put("b", new Integer(2));
map.put("c", new Integer(3));
//map.put(null, new Integer(1)); // not allowed
1) HashMap accepts null key, whereas Hashtable doesn't.
2) HashMap is not thread-safe, whereas Hashtable is thread-safe.
Map map = new Hashtable();
map.put("a", new Integer(1));
map.put("b", new Integer(2));
map.put("c", new Integer(3));
//map.put(null, new Integer(1)); // not allowed
Subscribe to:
Posts (Atom)