Recent Notes

Displaying keyword search results 11 - 20
Created by Dr. Xi on June 22, 2011 09:42:35    Last update: June 22, 2011 11:39:40
Demo code for CSV parsing with Skife CSV . Java code: import java.io.*; import java.util.*; import... Test with a simple CSV file: psmith01,CLASS2B,Peter Smith 1,YEAR2,1,N,ADVANCED,... The parser worked correctly: Line 1 has 11 values: |psmith01| |CLASS2B|... Test with a more complicated CSV file: "psmith01 abc", "CLASS2B " , " Peter... Result: Line 1 has 4 values: |psmith01 abc| | CLAS... The parser worked correctly. But notice that it counts the spaces outside of the quotes significant, and doing so consistently. One more test for spaces: "Smith, Jack" , "210-345-8888" "Smith, Jack"... Result: Line 1 has 2 values: | Smith, Jack | | 210... Add a new line in item two: "One", "Two ", "Three" Result: Line 1 has 2 values: |One| | Two| L...
Created by Dr. Xi on June 21, 2011 15:41:51    Last update: June 22, 2011 11:33:36
Demo code for CSV parsing with Apache Commons CSV parser . Java code: import java.io.*; import org.apache.commons.csv... Test with a simple CSV file: psmith01,CLASS2B,Peter Smith 1,YEAR2,1,N,ADVANCED,... Result: Line 1 has 11 values: |psmith01| |CLASS2B|... The parser worked correctly. Test with a more complicated CSV file: "psmith01 abc", "CLASS2B " , " Peter... Result: Line 1 has 4 values: |psmith01 abc| |CLASS... The third line is invalid input, but throwing a Java IOException is a bit grave. Also, the parser is not able to escape a backslash. Add a new line in item two: "One", "Two ", "Three" Result: Line 2 has 3 values: |One| |Two | |...
Created by Dr. Xi on June 21, 2011 15:54:00    Last update: June 22, 2011 11:33:09
Demo code for CSV parsing with SuperCSV parser . Java code: import java.io.*; import java.util.List; imp... Test with a simple CSV file: psmith01,CLASS2B,Peter Smith 1,YEAR2,1,N,ADVANCED,... The parser worked correctly: Line 1 has 11 values: |psmith01| |CLASS2B|... Test with a more complicated CSV file: "psmith01 abc", "CLASS2B " , " Peter... The parser messed up on all three lines: Line 1 has 5 values: |psmith01 abc| |CLASS... Using two lines input: "psmith01 abc", "CLASS2B " , " Peter... It generates an exception: Line 1 has 5 values: |psmith01 abc| |CLASS... Add a new line in item two: "One", "Two ", "Three" Result: Line 2 has 3 values: |One| |Two | |...
Created by Dr. Xi on June 22, 2011 07:12:02    Last update: June 22, 2011 11:32:02
Demo code for CSV parsing with OstermillerUtils . Java code: import java.io.*; import java.util.List; imp... Test with a simple CSV file: psmith01,CLASS2B,Peter Smith 1,YEAR2,1,N,ADVANCED,... The parser worked correctly: Line 1 has 11 values: |psmith01| |CLASS2B|... Test with a more complicated CSV file: "psmith01 abc", "CLASS2B " , " Peter... The parser messed up on all three lines: Line 1 has 6 values: |psmith01 abc| | "CLA... Putting a space before the first quote: "Smith, Jack","210-345-8888" It dismissed the quotes: Line 1 has 3 values: | "Smith| | Jack"| ... Add a new line in item two: "One", "Two ", "Three" Result: Line 1 has 2 values: |One| | "Two| ...
Created by alfa on June 07, 2011 11:34:26    Last update: June 07, 2011 11:36:37
This is an example that uses dynamic proxies to trace method calls (in logging) and print out elapsed times for them. Because dynamic proxies can only be generated for interfaces, the service classes must be implemented with interface-implementation pairs. Create services A and B. A.java : public interface A { public void service1()... AImpl.java : import java.util.Random; public class AImpl... B.java : public interface B { public void service1()... BImpl.java : public class BImpl implements B { public vo... The call trace proxy: import java.lang.reflect.*; class TraceProx... The performance proxy: import java.lang.reflect.*; class Performan... The service factory: import java.lang.reflect.*; public class Se... The test class: public class Test { public static void main... The output: Entering AImpl.service1 Entering BImpl.service1... The above example has no information...
Created by alfa on June 03, 2011 12:36:14    Last update: June 03, 2011 12:36:14
Thread local storage is typically used to associate state with a thread (e.g., a user ID or Transaction ID). This example, coming from Java API doc , assigns a unique id to each thread that calls getCurrentThreadId() . The actual usage is demonstrated with the Java concurrency package. import java.util.concurrent.Future; import java...
Created by alfa on May 27, 2011 11:19:29    Last update: May 31, 2011 07:56:26
This is a utility to convert a string value to one of the primitive type values. It is useful in Java reflection code where the value comes in as a string (e.g., from XML parsing), and the type of the value cannot be decided until runtime. import java.util.*; import java.lang.reflect.*;... Note: This method can be extended to convert string to more complex types by writing a converter for the destination type. For example, to convert string to date: public class DateConverter { public static ... Usage: Date d = (Date) ConvertUtil.convert("10/12/2010", ...
Created by Dr. Xi on April 26, 2011 20:12:01    Last update: April 28, 2011 15:28:12
An XML schema is a definition of XML files, in XML. It plays the same role as old-time DTDs. Overall, an XML schema file looks like this: <schema attributeFormDefault = (qualified | u... The attribute meanings: targetNamespace : The name space targeted by the current schema definition. It can be any URI. id and version : For user convenience, the W3C spec defines no semantics for them. xml:lang : Natural language identifier defined by RFC 3306 . attributeFormDefault and elementFormDefault : Set default values for the form attribute for attribute and element declarations. blockDefault and finalDefault : Set default values for the block and final attributes for attribute and element declarations. The W3C defined some built-in datatypes . Examples of primitive datatypes are: string ,...
Created by freyo on April 12, 2011 13:05:33    Last update: April 12, 2011 13:06:26
android:sharedUserId (from Android doc): The name of a Linux user ID that will be shared with other applications. By default, Android assigns each application its own unique user ID. However, if this attribute is set to the same value for two or more applications, they will all share the same ID — provided that they are also signed by the same certificate. Application with the same user ID can access each other's data and, if desired, run in the same process. Declare sharedUserId in AndroidManifest.xml : <manifest xmlns:android="http://schemas.android.co... Retrieve sharedUserId programmatically: import android.content.pm.PackageManager; impor...
Created by Dr. Xi on March 28, 2011 20:51:30    Last update: March 28, 2011 20:53:46
HTTP basic authentication is just Base64 encoded user name and password passed in as the Authorization header. So the following code works: // encode user name and password String credent... However, since JDK 1.2, there's a more Java friendly way: Authenticator.setDefault(new Authenticator() { ... Test code: import java.net.*; import java.io.*; pub...
Previous  1 2 3 4 Next