Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Thursday, March 29, 2012

Java encoding : UTF-8, Big5, x-MS950-HKSCS

Suppose you got a file in Big5 containing some HKSCS characters (e.g. 深水埗, 赤鱲角, etc). When your environment (Ref: Charset.defaultCharset( ) ) is either UTF-8 / Big5, you will never get the things work.

The proper encoding should be "x-MS950-HKSCS"

This situation happens when I'm try the same pieces of code in Java console / Eclipse console and JSP @ Tomcat. While JSP works fine, but sucks in console mode. Finally I found the JSP is actually using encoding "x-MS950-HKSCS".

To set environment charset in Eclipse:

Right click on the file that you are going to execute (e.g. Testing.java), then "Properties" → "Run/Debug Settings" → "Common" → "Encoding". Type "x-MS950-HKSCS" in the field.

What a pity!

Thursday, March 8, 2012

Regexp match with GenericValidator

Just a reminder to use GenericValidator for a quick way to do regexp matching in Java.

boolean rslt = org.apache.commons.validator.GenericValidator.matchRegexp(str, "(?i)abc|def|ghi");


Wednesday, February 8, 2012

Solution for java.io.NotSerializableException error when Tomcat reloads

To solve the Tomcat restart error, add <Manager> setting in <Context> section:

<Manager className="org.apache.catalina.session.PersistentManager" 
         saveOnRestart="false">
    <Store className="org.apache.catalina.session.FileStore"/>
</Manager>


References:

Sunday, March 27, 2011

Reading and saving UTF8 text file in Java

To read and save text file in Java, we need to add the "UTF-8" parameter in the Stream Reader.


File file = new File("1.txt");
BufferedReader reader = null;
try {
   reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
   String text = reader.readLine();
   String s1="\u6df1\u6c34\u57d7";
   String s2="深水埗";

   OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("2.txt"),"UTF-8");
   out.write(text+s1+s2);
   out.flush();
   out.close();

} catch (Exception e) {
   e.printStackTrace();
}

Sunday, September 20, 2009

Analyze JVM memory heap

To analyze how a Java web application is performed in terms of its memory usage, one can use Eclipse Memory Analyzer to do it.

Ref: Eclipse MAT

Monday, July 13, 2009

InetAddress.getByName( ) cached result

java.net.InetAddress.getByName( ) will cached the result, even when the host name IP is changed. To fix this, issue the following option to Java VM:

-Dsun.net.inetaddr.ttl=0

It sets the timeout of the DNS cache to zero.

Wednesday, April 16, 2008

Enable Java assertion

Just a soft reminder in case I want to use Java assertion. Given a class Cls:

> java -ea:Cls Cls

The comment will execute Cls which assertion enabled.

CSP on Apache

To add CSP to root if sort of funny. The following will NOT work for most cases !!     <LocationMatch "^/$">        Header s...