Monday, July 9, 2012

Determine whether Java version is 32-Bit or 64-Bit

Sun Java has provided a System property to determine whether installed version supports 32-Bit or 64 Bit named "sun.arch.data.model".


sun.arch.data.model=32 // 32 bit JVM
sun.arch.data.model=64 // 64 bit JVM
Try the following in your Java program: System.out.print(System.getProperty("sun.arch.data.model")) ; Other option is on command line use the following command, it will give you the details about the version and Bits:
java - version





C:\>java -version
java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b05)
Java HotSpot(TM) 64-Bit Server VM (build 20.6-b01, mixed mode)

Wednesday, December 21, 2011

Extract Numbers from a String


public static List extractNumsFromStr(String line) {
List numbers = new ArrayList();
Long no;
String number = null;
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(line);

while (m.find()) {
number = m.group();
no = Long.parseLong(number);
numbers.add(no);
}
return numbers;
}

Thursday, November 10, 2011

Convert XML file To String object

The following code will read the XML from file specified as method argument, and will return the String object containing the XML file contents, null otherwise.



public static String convertXMLFileToString(String filePath) {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
InputStream inputStream = new FileInputStream(new File(filePath));
org.w3c.dom.Document document = documentBuilderFactory.newDocumentBuilder().parse(inputStream);
StringWriter stwriter = new StringWriter();
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.transform(new DOMSource(document), new StreamResult(stwriter));
return stwriter.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}