Friday, September 21, 2007

List the names of all files in a particular directory

This code reads and prints the name of all the files and folders in a particular parent folder.
import java.io.File;

public class DirectoryReader {

public static void main(String[] args) {

File folder = new File("c:/");
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
System.out.println("File " + listOfFiles[i].getName());
} else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory " + listOfFiles[i].getName());
}
}
}
}

Wednesday, September 19, 2007

Tip: BlackBerry - Syncing PIM Data with Microsoft Outlook

By Amy Mayer
February 1, 2008
Setting up your Blackberry to synchronize with Microsoft Outlook means your contacts, appointments and tasks can be updated either from your smartphone or at your desk.

Here's how to do it:

Once you have the Blackberry Device Manager installed, launch it from your computer and click on Synchronize. Select the Configuration tab.

When you click on "configure synch" you'll be prompted with a box that allows you to select the feature (such as address book or tasks) and then select the software that's on your computer (in addition to Outlook, the Device Manager is prepared to work with Lotus Organizer, Novell GroupWise, Sage ACT! and others).

After selecting the appropriate software and whether you want the two machines to be synchronized or you simply want to import or export from one to the other, you can select Configure-->advanced settings to further customize your synchronization.

Here, you can decide which device overrides the other or if you want all data to be shared between the two. You can also apply filters and adjust the field mapping terms.

When you've got the settings the way you want them, click OK on the configuration box and Close on the synchronization box.

Now, when you click Synchronize from the Device Manager, you just have to click Synchronize Now and all your data will be shared. If you click the Automatic Synchronization box then you won't even need to tell the manager to synchronize, just connect your BlackBerry to your computer and your data will flow.

Monday, September 17, 2007

Tip: BlackBerry - Number Lock & CAPS Lock

By Amy Mayer
February 7, 2008
You BlackBerry’s NUM lock function can come in handy when you need to use your keypad as a number pad. While not super-obvious, it is easier to locate than your smartphone’s CAPS lock.

# To launch num lock:

Hold the # (also the shift) key down, then press ALT.

When you're ready to return to letters:

simply press the ALT key.

# CAPS lock takes a bit more effort launch. If all you want is one capital letter, just press the # key, then the letter you want capitalized. But for CAPS lock, the key combination is :

# and the green phone button.

That's right, you hold down the #, or shift, key and then you press the green button as though you were going to make a phone call.

Exit the phone screen and you'll go back to your message with CAPS lock on. To turn it off:

again press # and the green phone.

Cumbersome? Yes. But maybe just enough to calm your rage so you don’t send that message in all caps after all.

How to use Comparable Interface

List of objects that implement this interface can be sorted automatically by sort method of the list interface. This interface has compareTo() method that is used by the sort() method of the list.

In this code Employee class is implementing Comparable interface and have method compareTO(). ComparableDemo.java is showing the use of this interface. This class first makes a list of objects of type Employee and call sort method of java.util.Collections, which internally uses compareTo() method of Employee class and sort the list accordingly.

Employee.java
public class Employee implements Comparable {

int EmpID;
String Ename;
double Sal;
static int i;

public Employee() {
EmpID = i++;
Ename = "dont know";
Sal = 0.0;
}

public Employee(String ename, double sal) {
EmpID = i++;
Ename = ename;
Sal = sal;
}

public String toString() {
return "EmpID " + EmpID + "\n" + "Ename " + Ename + "\n" + "Sal" + Sal;
}

public int compareTo(Object o1) {
if (this.Sal == ((Employee) o1).Sal)
return 0;
else if ((this.Sal) > ((Employee) o1).Sal)
return 1;
else
return -1;
}
}

ComparableDemo.java
import java.util.*;

public class ComparableDemo{

public static void main(String[] args) {

List ts1 = new ArrayList();
ts1.add(new Employee ("Tom",40000.00));
ts1.add(new Employee ("Harry",20000.00));
ts1.add(new Employee ("Maggie",50000.00));
ts1.add(new Employee ("Chris",70000.00));
Collections.sort(ts1);
Iterator itr = ts1.iterator();

while(itr.hasNext()){
Object element = itr.next();
System.out.println(element + "\n");

}

}
}

Output:

EmpID 1
Ename Harry
Sal20000.0

EmpID 0
Ename Tom
Sal40000.0

EmpID 2
Ename Maggie
Sal50000.0

EmpID 3
Ename Chris
Sal70000.0

Tuesday, September 11, 2007

Difference between String StringBuffer and StringBuilder

String is immutable whereas StringBuffer and StringBuilder can change their values.

The only difference between StringBuffer and StringBuilder is that StringBuilder is unsynchronized whereas StringBuffer is synchronized. So when the application needs to be run only in a single thread then it is better to use StringBuilder. StringBuilder is more efficient than StringBuffer.

Criteria to choose among String, StringBuffer and StringBuilder

1. If your text is not going to change use a string Class because a String object is immutable.
2. If your text can change and will only be accessed from a single thread, use a StringBuilder because StringBuilder is unsynchronized.
3. If your text can changes, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous.

Saturday, September 08, 2007

Tip: Using Your BlackBerry as an MP3 Player

By Amy Mayer
February 8, 2008
Click to View
Now that you've grown an awesome digital music library on your desktop—maybe it's even replaced CDs as your music-medium of choice—isn't it time you enjoyed choice selections on your BlackBerry? Whether your all-in-one replaces your iPod remains to be seen, but to get music onto your BlackBerry, you'll need to connect it to your computer and launch the Device Manager.

To maximize the amount of music you can transfer, and minimize the impact on your BlackBerry's device memory, install a media card, the larger the capacity the better. Then, activate mass storage mode on the BlackBerry by selecting Options from the home screen and then Advanced Options„³Media Card„³Mass Storage Mode Support„³ON.

(You can use the Device Memory to store your music files, but they will take up considerable room quickly.)

From the Device Manager, click on Media Manager: The Media Manager will open a file transfer screen. Use the top-left corner to navigate to the place where your music files are stored on your computer. On the lower left area, indicate if you wish to store to a media card or your Device Memory.

When you select folders in the upper-left corner, the contents appear on the upper-right side. Select songs you want to copy and use the "Copy Down" button to place them on your BlackBerry. You'll be prompted with the question of whether to convert or not. Converting should reduce the strain on your BlackBerry's resources if you're using Device Memory. Whether you'll hear a difference after conversion depends on your ears, your device's speaker and whether you're using a headset.

To playback on the BlackBerry, select Media from the home screen. Then select Music. If you haven't placed your music in this folder, you may need to browse to find your music folder. But once you do, just click a file to play it.

The amount of music you can store will be limited by what else you're keeping on your device. But since it's quick and easy to copy files, you can always trash the tunes you're bored with and copy on some new ones. As for listening, if those around you tire of hearing your taste over the built-in speaker, then that's another reason to consider a headset.

Friday, September 07, 2007

How to use HashSet

HashSet class is a concrete implementation of Set interface. It creates a collection that uses a hash table for storage. Hash table stores information by using a mechanism called hashing. In hashing, the informational content of a key is used to determine a unique value, called its hash code. The hash code is then used as an index at which the data associated with the key is stored. The transformation of key into its hash code is performed automatically. You never see the hash code itself. The advantage of hashing is that it allows the execution time of basic operation, such as add(), contains(), remove(), and size() to remain constant even for large sets.

HashSet is not synchronized. If more than one thread wants to access it at the same time then it must be synchronized externally.

This code shows the use of HashSet. This will identify the number of duplicate words in a String. The String is passed as command line arguments.

Add() method of the HashSet add the object into the storage if it is not already present.
import java.util.*;

public class FindDups {

public static void main(String[] args) {

Set s = new HashSet();

for(int i=0; i if(!s.add(args[i]))
System.out.println("Duplicate detected : " + args[i]);
}

System.out.println(s.size() + " distinct words detected : " + s );
}
}

Run the program: C:\> java FindDups i came i came i conquered

Output Screen :

Duplicate detected: i
Duplicate detected: i
4 distinct words detected : [came,saw,conquered,i]