Posts

Showing posts with the label No Meaning of Life

Java object serialization with Xstream

It's an intresting tool. So I'd like to share some of the facts about it. Xstream is used to convert java objects to xml and vice versa. We need this kind of conversion because object persistance and data transportation can be done very easily with xml. ----------------------------------------------------------------------------------------- Object -> XML by Using Xstream libs you can directly convert any object to xml doc. String xml = xstream.toXML( Object o ); ---------------------------------------------------------------- XML -> Object XStream xs = new XStream(new DomDriver()); FileInputStream fis; ObjectInputStream in = null; fis = new FileInputStream("Path to the xml file"); xs.alias("person", Person.class); // give alias given to Person.class try { in = xs.createObjectInputStream(fis); // Convert happens here } catch (IOException e) { e.printStackTrace(); ...

Thousend seperator function - (1,00,000)

Normally if we write a number that is more than 1000, we normally write it with thousend seperator commas other than write it at once. Eg : If we want to write 203465397465 we write it as number : 203,465,397,465 this way we can read the number more easily than other way. So after adding commas to that number the number no longer be a in number format. We cant add, divide, substract.....etc. public String convertThousendsWithCommas(String num) { String pattern = "#,###.##;-#,###.##"; //pattern that we want double value = 0.0; String out = null; try { value = Double.parseDouble(num); NumberFormat nf = NumberFormat.getInstance(); if (nf instanceof DecimalFormat) { DecimalFormat df = (DecimalFormat) nf; df.applyPattern(pattern); out = df.format(value); } } catch (NumberFormatException n){ System.out.print("not a number:...