Posts

Showing posts with the label Java

Java , Java 8 and RxJava

Image
Hello programmers...!!!.Today we'll have our conversation on something "Advanced".You know Java.You know Java ...,5,6,7.But what on earth is Java 8 and RxJava. Java 8,as the name suggests,the 8th update of the super awesome Java language.The problem is RxJava...something new right??  Okay without further delay we'll discuss about the topic. As I already said Java 8 is the latest revolutionary release of Java. There are many useful and awesome new features included in Java 8 which make our life easier and less complicated. Following you can identify some of the main changes which have taken place in the latest release. Lamda Expressions(Oracle we don't need this.We have Python) Reapeating annotations Improvements in Collection class Performance improvement for HashMap Integration of Stream API to the Collections API New Stream API New Date/Time API Nashron new Javascript engine (Rhino to Nashron) Method references Default Methods Improved type Interfac...

Strings , I/O and formatting (Part-5 File I/O)

Image
Let's start coding on what we have learnt in the previous post. File f=new File("me.txt"); System.out.println(f.exists()); Here we created a File object and checked for the presence of the file using f.exists() method.If you don't have a me.txt inside the project folder this gives false as the output and if you do have  true as the output.Let's move on and create a new file. boolean a = f.createNewFile(); System.out.println(a+" "+f.exists()); Here we created a new file and check whether the process is successfully finished and the file is created.Note that createNewFile() creates a new file if it doesn't already exists.If the file exists you get false as the output from the above method. One thing to remember is that you have to put the file operation codes inside a try catch block as the operations declare checked exceptions.Alright...!!now we'll start writing text to files. File f=new File("me.txt"); FileWriter...

Strings , I/O and formatting (Part-4 StringBuffer and StringBuilder,File I/O)

Image
In the previous post we have discussed about some basic facts about StringBuffer and StringBuilder classes.At the end we have instantiated a StringBuffer object,applied some changes and logged the output and saw the differences between String and StringBuffer classes. Same rules apply to the StringBuilder class. StringBuilder a=new StringBuilder("hasitha"); a.append(" is a good boy"); System.out.println(a); Prints out " Hasitha is a good boy ".Now you are completely aware about how StringBuffer and StringBuilder classes behave when handling strings.Let's study some important methods in these classes.You have already have an idea about the append method.Note that append is a synchronized method for StringBuffer class.As in the previous post I am going to provide you the link for all the methods in StringBuffer and StringBuilder class.(Self study is the best....!!!!) StringBuffer https://docs.oracle.com/javase/7/docs/api/java/lang/StringB...

Strings , I/O and formatting (Part-3 StringBuffer and StringBuilder)

Image
I know you are an expert in Java string class now. But when handling an extremely dynamic environment which needs lots of modifications in a string and it's characters we need to use some additional techniques. StringBuffer and StringBuilder come to our help in these situations. As we discussed earlier strings are immutable objects.So when you change or modify string variables there is a tendency that you leave traces of used string objects in the memory which causes a memory overload or a memory leak.We can reduce this risk to a great extent using StringBuffer and StringBuilder in programming . A general use of StringBuffer and StringBuilder class is to handle File I/O .There you have to deal with some ever-changing and large streams which need a unique method of memory handling and memory management. StringBuffer and StringBuilder can handle this large amounts of characters as blocks,pass one block and reuse the used memory portion again without allocating a new space...

Strings, I/O and formatting (Part-1)

Image
"Hey Isla Are you serious??Strings,IO and formatting???"Hmmm..yeah I agree with you.This is basic Java.Anyway no problem we'll go further and discuss and see how much you know about Java Strings,I/O and formatting.We'll start with a little background information about strings. Strings are immutable objects.(You know what I mean) String is a 16-bit Unicode character In java string is an object.You can create a new instance using new keyword String myString=new String(); This creates a new string object and assign it to myString reference variable.Cool..Now we need to assign a value to the new variable.Let's do it like this. myString="Hasitha"; And we are done.Now we have a string variable with a value.Is this the only way we can do this.Absolutely no.String class has a number of constructors.We can do the same thing as follows. String myString=new String("Hasitha"); String myString="Hasitha"; Hmmm...Note that there...

Merging multiple xmls using Java (Part-2)

Image
In the previous post we have discussed about how we can collect tags by name in two xmls. NodeList fNodes, sNodes;fNodes = fMsg.getElementsByTagName("person");sNodes = sMsg.getElementsByTagName("person"); Now we have fNodes and sNodes variables with elements by tag inside. We'll append the elements of one xml to the other xml. for (int i = 0; i < sNodes.getLength(); i = i + 1) {       Node n = (Node) fMsg.importNode(sNodes.item(i), true);       fNodes.item(i).getParentNode().appendChild(n);} Okay....!!!!!Everything is okay now.All we have to do is get the output from above implementation.First we'll create a StringWriter object and parse the fMsg object to it.Here we have to use the Transformer object in order to build the new xml. StringWriter buf = new StringWriter();Transformer transformer = TransformerFactory.newInstance().newTransformer();transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");tr...

Merging multiple xmls using Java (Part-1)

Image
In the previous post we have completed our implementation on the "xml splitter". So in this post we'll discuss about how we can merge multiple xmls using Java.  Assume you have following xmls. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>      <person>     <id>person0</id>     <name>name0</name>     <age>age0</age>  </person> <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <person>     <id>person1</id>     <name>name1</name>     <age>age1</age>  </person> When the xpath is given as //person you want these two xmls to be merged to one xml from <person></person> tags.The final output should look like this. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>           <persons> ...

Split a large XML into pieces using Java(Part-2)

Image
In the previous post we discussed about building a parser for parsing xml. At the end of the previous post you are like "WTH is this new XmlErrorHandler() thing??".Basically what it does is that handle the errors inside the xml.(Missing tags and schema errors).So let's implement the error handler class. public class XmlErrorHandler implements ErrorHandler {       public void warning(SAXParseException e) throws SAXException {            //print the exception       }       public void error(SAXParseException e) throws SAXException {            //print the exception       }       public void fatalError(SAXParseException e) throws SAXException {           //print the exception       } } Okay now the fun part..!!We need to split the xml from <person></person>  t...