other register

Friday, August 17, 2007

Retrieve a text file from a web address


Retrieve a text file from a web address and write it to a local file.

// Retrieve the web address from the property file, in which you can change the web address without changing the Java code

Properties properties = new Properties();
try {
properties.load(new FileInputStream(""));
} catch (IOException e){
System.out.println("Can't fild");
String webAddress= properties.getProperty("webAddress");

// Construct the url
URL url = new URL(webAddress);

// Establish the connection
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

// Retrieve the response status code
int statusCode = httpURLConnection.getResponseCode();

if (statusCode == 200){
// Create an empty file
File file = new File ("folderName" + "/" + "fileName" + ".extensionName");
if (file.exists()) {
// Create a buffered writer for output
BufferedWriter output = new BufferedWriter(new FileWriter(file, true));

// Create a buffered reader to retrieve content from the web address
BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream)httpURLConnection.getContent()));

String lLine = "";

// Read web content line by line and write it to the output file
while ((line = reader.readLine()) != null) {

// Close the buffered writer

Thursday, August 16, 2007

Vi: Search and Replace

Search (Wraped around at end of file):
  Search STRING forward :   / STRING.
Search STRING backward: ? STRING.

Repeat search: n
Repeat search in opposite direction: N (SHIFT-n)

Replace: Same as with sed, Replace OLD with NEW:

First occurrence on current line: :s/OLD/NEW

Globally (all) on current line: :s/OLD/NEW/g

Between two lines #,#: :#,#s/OLD/NEW/g

Every occurrence in file: :%s/OLD/NEW/g

Friday, August 10, 2007

svn merge example

Problem Solving:

Merge code from one branch to the trunk.

The release candidate server has the code from trunk.

In release_candidate server's source code directory:

svn merge --dry-run -r rev1:rev2 branch_url

# rev1 is the revision number when first branch out from trunk
# rev2 is the latest revision number from the branch

svn merge -r rev1:rev2 branch_url

svn status | grep "C "

# see any conflicts after merge, "C" stands for conflict

# for those conflict files, edit them to fix the conflict.

# filename.working is the local copy (here is the trunk code)
# filename.right is the latest from the branch
# filename.left is the original from the brach when first branched out from trunk

# happy

svn resolved filename

svn commit -m "commit rev1:rev2 branch_url"

# commit the merged code to the trunk

# done

For more svn knowledge, please see SVN RedBook

Sorting Map

The following definition is from Java API.


Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.


Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key k is reinserted into a map m if m.put(k, v) is invoked when m.containsKey(k) would return true immediately prior to the invocation.)


Red-Black tree based implementation of the SortedMap interface. This class guarantees that the map will be in ascending key order, sorted according to the natural order for the key's class (see Comparable), or by the comparator provided at creation time, depending on which constructor is used.

Problem Solving:
One university department has three levels of courses, namely undergraduate, postgraduate taught, and postgraduate research to offer. The department wants to show these courses in alphabatical order within respective level of course. The order of the level should be: undergraduate, postgraduate taught, postgraduate research.

LinkedHashMap courseMap = new LinkedHashMap();
TreeMap ugCourseTreeMap = new TreeMap();
courseMap.put("UG", ugCourseTreeMap);

TreeMap pgtCourseTreeMap = new TreeMap();
courseMap.put("PGT", pgtCourseTreeMap);

TreeMap pgrCourseTreeMap = new TreeMap();
courseMap.put("PGR", pgrCourseTreeMap);

// looping begins
// courseName is a String
// course is a class

ugCourseTreeMap.put(courseName, course);
pgtCourseTreeMap.put(courseName, course);
pgrCourseTreeMap.put(courseName, course);

// looping end

Iterator it = ugCourseTreeMap.values().iterator();
while(it.hasNext()) {


/**************No need to do the following************
/**************Because TreeMap is ordered*************
ArrayList keysUG = new ArrayList();

ArrayList keysPGT = new ArrayList();

ArrayList keysPGR = new ArrayList();

// Iteration example
Iterator it = ugCourseMap.keySet().iterator();
while (it.hasNext()) {


Search a keyword in a directory recursively

If you want to search a keyword in files of the current directory and its sub directories:


then do:

find . | xargs grep whatever

Wednesday, August 08, 2007

Monday, August 06, 2007

The use of Link in HTML

Can't add Alt in background image in CSS

When using background: url(/imagesource); in CSS, please be aware that there no way to add Alt text. Therefore, using normal html text instead.

Friday, August 03, 2007

Replacing string in multiple files without changing filenames.


for file in /path/to/my/files/*
sed 's/stringA/stringB/g' $file > $
mv $ $file
exit 0

Make the script executable
$ chmod u+x

Then run it (assuming it's in the current directory)
$ ./

For more sed examples, please have a look at the IBM articles

Thursday, August 02, 2007

WTP Tutorials - JavaServer Faces Tools Tutorial

Any Eclipse fans who want to learn JSF should have a look at this tutorial.

Make sure you say "Hello World!" ;)