Feb
07

read a file into List

There are many ways to read file into a List in Java.

Scanner s = new Scanner(new File("filepath"));
ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()){
    list.add(s.next());
}
s.close();

commons-io

List<String> lines = FileUtils.readLines(new File("input.txt"), "utf-8");

guava

List<String> lines =  Files.readLines(new File("input.txt"), Charset.forName("utf-8"));

Java 7.0

List<String> list = Files.readAllLines(new File("input.txt").toPath(), Charset.defaultCharset() );
Feb
07

platform independent separators in java

line separator (Windows: “\r\n”, UNIX: “\n”)

1. BufferedWriter newline()
2. System.lineSeparator()
3. System.getProperty(“line.separator”)

file separator file.separator(windows:”\”; unix:”/”)

1. BufferedWriter newline()
2. System.getProperty(“file.separator”)
3. File.pathSeparator

Annotation: In Windows: “D:\codehamster”, you can change it to “D:/codehamster” or “D:\\codehamster”

Feb
06

the useful subclass for BitSet and FastBitSet

BitSet and FastBit will always change themselves if they do some set operations, just like and, or, andNot. To avoid such cases, you should write a extended classes:

For BitSet: MyBitSet

import java.util.BitSet;

/**
 * @author Conny Gu This Class extends from Class BitSet, it won't change
 *         itself while it does some operations.
 * 
 */

public class MyBitSet extends BitSet {

	public MyBitSet() {
		super(0);
	}

	public MyBitSet(int nbits) {
		super(nbits);
	}

	/**
	 * This myand() funktion will return a BiSet and itself won't be changed.
	 * 
	 * @param b
	 * @return MyBitSet
	 */
	public MyBitSet myand(MyBitSet b) {
		MyBitSet a = (MyBitSet) this.clone();
		a.and(b);
		return a;
	}

	public MyBitSet myor(MyBitSet b) {
		MyBitSet a = (MyBitSet) this.clone();
		a.or(b);
		return a;
	}

	/**
	 * This myandNot() funktion will return a BiSet and itself won't be changed.
	 * 
	 * @param b
	 * @return MyBitSet
	 */

	public MyBitSet myandNot(MyBitSet b) {
		MyBitSet a = (MyBitSet) this.clone();
		a.andNot(b);
		return a;
	}

}

For FastBitSet: MyFastBitSet

import javolution.util.FastBitSet;

/**
 * @author Conny Gu This Class extends from Class FastBitSet, it won't change
 *         itself while it does some operations.
 * 
 */

public class MyFastBitSet extends FastBitSet {

	public MyFastBitSet() {
		super();
	}

	public MyFastBitSet(FastBitSet b) {
		super();
		this.addAll(b);

	}

	
	public MyFastBitSet myand(MyFastBitSet b) {

		MyFastBitSet a = new MyFastBitSet();

		a.addAll(this);

		a.and(b);

		return a;

	}

	public MyFastBitSet myor(MyFastBitSet b) {

		MyFastBitSet a = new MyFastBitSet();

		a.addAll(this);

		a.or(b);

		return a;

	}

	public MyFastBitSet myandNot(MyFastBitSet b) {

		MyFastBitSet a = new MyFastBitSet();

		a.addAll(this);

		a.andNot(b);

		return a;

	}

}
Feb
06

Example for Javolution FastBitSet

Javolution is a High-performence java library, it is not only fast but also very comfortable to use.

FastBitSet of Javolution is similar to BitSet of Java native Bib. But there are still some differences in usage:

1. BitSet is cloneable, but FastBitSet not.

2. FastBitSet has an own Number Class: Index

public static void main(String[] args) {

		FastBitSet fbs = new FastBitSet();
		
		fbs.set(300);
		fbs.set(200);
		fbs.set(450);

//              copy a FastBitSet
		FastBitSet fbs2 = new FastBitSet();
                fbs2.addAll(fbs);
		
//		print all the data in FastBitSet
		for(int i=fbs.nextSetBit(0); i>=0; i=fbs.nextSetBit(i+1)){
			System.out.println(i);
		}
		
//		Example for how to use "toArray"
		Index[] arr = fbs.toArray(new Index[0]);
		
		for(Index i : arr){
			System.out.println(i);
		}

}
Jan
04

HTML5 Audio and Video Tags

for Audio

<audio controls>
    <source src="http://www.codehamster.com/dongdong/fragrance_of_flowers.mp3" type="audio/mp3">
</audio>

for Video

<video controls> 
    <source src="http://www.codehamster.com/dongdong/Neujahr2014.mp4" type="video/mp4">
</video>

You can directly see and the try the cods with JS Bin

Audio and Video Tags for HTML5

Jan
01

How to connect SAP HANA Studio to SAP HANA Sever on AWS

After many hours of research and self-studies I found a way to connect my local HANA Studio
to the HANA Server on AWS.
1.) Start your Instances on AWS.
2.) Go to the AWS Dashboard and click on INSTANCES > Instances to list all running instances.
3.) Choose the SAP HANA Server and look at the Description tab.
4.) Remember the long name behind Security groups.
5.) Click on NETWORK&SECURITY > Security Groups.
6.) Choose the line with that long name from step 4.
Click on the Inbound Tab.
7.) Create a new “Custom TCP rule” with Port range = 30015 and Source 0.0.0.0/0.
Add Rule and Apply Rule Changes.

Dec
31

IE 10 still doesn’t support “onchange” event in HTML5!

HTML5 has a lot of nice feature, but the cross browser support is still not so ideally.

Just like:

If you want to disable the fields in a “fieldset” and dynamically enable them when some condition is met, the best way is to use a radio button or a checkbox.

You can add a “disabled” attribute on the nested fieldset elements, and use “onchange” event on the radio button or checkbox to controll the “disabled” attribute.

But it still doesn’t work in IE 10!!!

The following code works under chrome and firefox perfectly but not IE:

<form>
    <fieldset>
        <legend>Store Credit Card</legend>
        <p><label>Name displayed on your card:<input name="fullName"
            required></label></p>
        <fieldset name="accountNum" disabled>
            <legend>
                <label>
                    <input type="radio" name="accountType"
                           onchange="form.accountNum.disabled = !checked;
                           form.accountLetters.disabled=checked">My account is a
                           12-digit number
                </label>
            </legend>
            <p><label>Store card number: <input name="cardNum"
                required></label></p>
        </fieldset>
        <fieldset name="accountLetters" disabled>
            <legend>
                <label>
                    <input type="radio" name="accountType"
                           onchange="form.accountLetters.disabled = !checked;
                           form.accountNum.disabled=checked">My account includes
                           letters and numbers
                </label>
            </legend>
            <p><label>Store card code: <input name="cardLetters"></label></p>
        </fieldset>
    </fieldset>
</form>
Dec
24

How to make your website speak?

You can make your website or blog speak without any complicated technology. The easiest way is to use the tts-cloud service, but the most tts cloud services are costly. I tried a lot of them, at the end I found the VoiceRSS is really excellent and free for normal users.

Steps:

1. go to http://www.voicerss.org

2. register a free account

3. get the API key

4. read the API

5. put the code in your blog 😀

6. finish! 😀


Voice RSS - Free online text-to-speech service

Dec
10

set security level in java control panel

Some times you might face the problem that java application can not be allowed to run in your webbrowser:

Error Message: Application Blocked, Your security settings have blocked a local application from runing.

Application Blocked

What can you do at this time?

Just go to java control panel and then set the security level.

Win key+W -> search “java” -> set your desired security level.

java_control_panel