Recent Notes
Displaying keyword search results 1 - 10
Created by Dr. Xi on March 08, 2012 12:13:57
Last update: March 08, 2012 12:13:57
This example creates an instance of XMLGregorianCalendar and converts it to java.util.Date :
import java.util.Date;
import javax.xml.datatyp...
Created by Dr. Xi on May 02, 2011 15:59:37
Last update: February 25, 2012 09:16:37
This code snippet gets the default keystore used by the Java keytool and displays the list of aliases along with the key type (certificate or private key).
import java.io.File;
import java.io.FileInputSt...
The default keystore used by the above code is: $HOME/.keystore .
Created by magnum on August 11, 2010 19:22:06
Last update: November 28, 2011 08:16:22
import java.text.DateFormat;
import java.text.S...
Output:
Date LONG format: November 28, 2011
Date MEDIUM...
Created by Dr. Xi on November 11, 2011 10:05:22
Last update: November 11, 2011 10:12:01
This is an HTML image tag filter using Java regex. It takes a string, finds the img tags, replaces the src attribute with one provided by the filter, then adds a class name to the class attribute.
import java.util.regex.*;
import java.io.*;
...
Test file:
<div id="HTML snippet">
<img src="img/big/txt-m...
Created by Dr. Xi on July 27, 2011 08:55:34
Last update: July 27, 2011 08:55:34
It is OK to remove elements from a list with Iterator , but you get UnsupportedOperationException if the list is created with Arrays.asList :
import java.util.*;
public class IteratorRe...
Prints:
ArrayList test:
================
List size: ...
Created by alfa on July 15, 2011 13:25:45
Last update: July 15, 2011 13:25:45
Read the whole contents of a file into a String. It's better to read the whole file as bytes and convert to String than to read the file line by line and concatenate the lines.
String getFileContents(String fileName) throws...
Using java.nio :
import java.io.FileInputStream;
import java...
Created by Dr. Xi on July 15, 2011 09:25:15
Last update: July 15, 2011 09:25:15
Some methods to search for a substring within a string:
To know that a substring indeed exists within a string:
boolean found = wholeString.contains(substring);
To find where the substring is contained:
int index = wholeString.indexOf(substring);
If the substring is regex:
boolean match = wholeString.matches(".*" + substri...
Case insensitive match: convert both whole string and substring to lowercase, then compare. Or, use case insensitive flag for regex.
Test code:
import java.util.regex.*;
public class Stri...
Created by Dr. Xi on July 14, 2011 09:28:57
Last update: July 14, 2011 09:28:57
Java arrays are fixed size, so you have to make a new array with smaller size and copy the data.
For JDK6 and above:
// import java.util.Arrays;
newArray = Arrays.c...
Before that (using Object as example):
Object[] newArray = new Object [newSize] ;
Syst...
Created by Dr. Xi on August 13, 2007 20:27:11
Last update: July 13, 2011 16:20:28
Sample code:
import java.util.*;
public class TestArrayL...
If you use iterators, the for loop is equivalent to:
for (Iterator<String> i = l.iterator(); i.hasNext(...
The simplified for loop (or, for-each loop) can be used for arrays or objects that implement java.lang.Iterable .
Note that by using generics, there's not need to down cast. But new for loop syntax doesn't down cast either. If List<String> is changed tp List<Object> , the code doesn't compile.
Created by Dr. Xi on July 13, 2011 16:18:05
Last update: July 13, 2011 16:18:05
The goal is to read a file like this:
for (String line: textFileReader) {
// do s...
This is the code:
import java.io.*;
import java.util.Iterator;
...