<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-186002073186625662</id><updated>2012-02-15T22:22:07.660-08:00</updated><title type='text'>Technology Blogs</title><subtitle type='html'>This blog intends to be a quick reference guide to Java developers looking out for latest technological practises, tricks &amp;amp; tips. We are always looking for contributors. Thus, if you have stumbled on this site by chance, and would want to start blogging yourself, send a mail to shreyas735@gmail.com with a brief description of yourself.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://shreyas735.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://shreyas735.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Parth</name><uri>http://www.blogger.com/profile/05955499680718949722</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>18</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-186002073186625662.post-1679409751469437806</id><published>2008-07-18T17:43:00.000-07:00</published><updated>2008-07-20T09:45:06.737-07:00</updated><title type='text'>SAX Parser</title><content type='html'>Event driven XML parsing: XML parsing can be done in a variety of ways. But mainly it can be classified into two main parts -Event driven and DOM Tree Structure driven. In this blog we discuss an implementation of Event driven XML parsing.&lt;br /&gt;&lt;br /&gt;Event driven approach is used when the XML to be parsed is a large size xml, because it will generate events every now and then and give call backs to your event handler class. Let us now see how this can be accomplished.&lt;br /&gt;&lt;br /&gt;The main interfaces we are using here are XMLReader and ContentHandler.&lt;br /&gt;&lt;br /&gt;XMLReader is the main interface that has methods to set the event handler and initiating the parse. If you are wondering who provides the class for this interface, it is the XML parsers SAX2 driver. XMLReaderFactory fetches the actual class and gives us a handle to it.&lt;br /&gt;&lt;br /&gt;Besides the above mentioned tasks, XMLReader allows us to to set and query features and properties in the parser, to register event handlers for document processing, and to initiate a document parse.&lt;br /&gt;&lt;br /&gt;All SAX interfaces are assumed to be synchronous: the parse methods must not return until parsing is complete, and readers must wait for an event-handler callback to return before reporting the next event.&lt;br /&gt;&lt;br /&gt;ContentHandler is the main interface that most SAX applications implement: if the application needs to be informed of basic parsing events, it implements this interface and registers an instance with the SAX parser using the setContenthandler method. The parser uses the instance to report basic document-related events like the start and end of elements and character data.&lt;br /&gt;&lt;br /&gt;Let us now see an implementation of the SAXParser.&lt;br /&gt;&lt;br /&gt;Code Sample for the event handler class - An instance of this class would be used to set the content handler for the XMLReader interface. Note the callbacks in the methods. These would get called as events as soon as you initiate parse of XMLReader interface.In the TODO section you can embed your code.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public class XMLContentHandler implements ContentHandler &lt;br /&gt;{ &lt;br /&gt;&lt;br /&gt;public void characters(char[] arg0, int arg1, int arg2) throws SAXException &lt;br /&gt;{ &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;public void endDocument() throws SAXException &lt;br /&gt;{ &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;public void endElement(String arg0, String arg1, String arg2) throws SAXException &lt;br /&gt;{ &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;public void endPrefixMapping(String arg0) throws SAXException &lt;br /&gt;{ &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException &lt;br /&gt;{ &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;public void processingInstruction(String arg0, String arg1) throws SAXException &lt;br /&gt;{ &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;public void setDocumentLocator(Locator arg0) &lt;br /&gt;{ &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;public void skippedEntity(String arg0) throws SAXException &lt;br /&gt;{ &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;public void startDocument() throws SAXException &lt;br /&gt;{ &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException &lt;br /&gt;{ &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;public void startPrefixMapping(String arg0, String arg1) throws SAXException &lt;br /&gt;{ &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Finally the method that creates a XMLReader and initiates the parse of XML document. The variable xmlPath has to contain the system specific path of XML file. &lt;br /&gt;&lt;br /&gt;public void parseXML() &lt;br /&gt;{ &lt;br /&gt;try &lt;br /&gt;{ &lt;br /&gt;InputSource xmlSource = new InputSource(); &lt;br /&gt;xmlSource.setCharacterStream(new FileReader(new File(xmlPath))); &lt;br /&gt;XMLReader xmlReader = XMLReaderFactory.createXMLReader(); &lt;br /&gt;xmlReader.parse(xmlSource); &lt;br /&gt;xmlReader.setContentHandler(new XMLContentHandler()); &lt;br /&gt;} &lt;br /&gt;catch (FileNotFoundException e1) &lt;br /&gt;{ &lt;br /&gt;} &lt;br /&gt;catch (IOException e2) &lt;br /&gt;{ &lt;br /&gt;} &lt;br /&gt;catch (SAXException e3) &lt;br /&gt;{ &lt;br /&gt;} &lt;br /&gt;catch (Exception e4) &lt;br /&gt;{ &lt;br /&gt;} &lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/186002073186625662-1679409751469437806?l=shreyas735.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shreyas735.blogspot.com/feeds/1679409751469437806/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=186002073186625662&amp;postID=1679409751469437806' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/1679409751469437806'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/1679409751469437806'/><link rel='alternate' type='text/html' href='http://shreyas735.blogspot.com/2008/07/sax-parser.html' title='SAX Parser'/><author><name>Parth</name><uri>http://www.blogger.com/profile/05955499680718949722</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-186002073186625662.post-4741128178936535666</id><published>2008-07-18T15:27:00.000-07:00</published><updated>2008-07-20T09:48:06.058-07:00</updated><title type='text'>Factory Patterns</title><content type='html'>Dynamic Method dispatch and Factory patterns&lt;br /&gt;Factory patterns are almost always used in conjunction with function overloading feature in Java. This feature is also called Dynamic method dispatch. This is a runtime java feature wherein hierarchy of classes exist each providing an implementation of the same methods. The decision of which implementation to load and execute is taken entirely at runtime.&lt;br /&gt;Let us look at an example to understand it clearly:&lt;br /&gt;Consider a class called ‘Shell’ with a method called execute() which is supposed to execute shell commands. Now the commands can be meant for the local system shell or a remote shell. Thus we have two more classes providing implementation for the method ‘execute()’ – RemoteShell and LocalShell.&lt;br /&gt;At runtime let us assume that the utility takes a decision of which shell to use on the basis of some input from some XML file. We can very easily write the logic that decides for us which of the two subclasses are meant to be executed in the current scenario. Now this particular piece of code would get repeated at every place we want to invoke the Shell. This might also include the initialization logic of each class.&lt;br /&gt;Ex.&lt;br /&gt;If remote required&lt;br /&gt;Initialize the RemoteShell class.&lt;br /&gt;Call the execute.&lt;br /&gt;Else If local required&lt;br /&gt;Initialize the LocalShell class.&lt;br /&gt;Call the execute.&lt;br /&gt;End If&lt;br /&gt;It is this repetition of code that factory patterns can help us avoid. The implementation is simple.&lt;br /&gt;Create a ShellFactory class with a method for getting Instance of the base class Shell. On the basis of what input you pass to the method, the ShellFactory initializes one of the two subclasses and returns a reference&lt;br /&gt;Shell getInstance(arguments) returns base class object Shell&lt;br /&gt;{&lt;br /&gt;If Remote&lt;br /&gt;Init and Return new RemoteShell object&lt;br /&gt;If Local&lt;br /&gt;Init and Return new LocalShell object&lt;br /&gt;}&lt;br /&gt;Usage:&lt;br /&gt;ShellFactory.getInstance(args).execute()&lt;br /&gt;Benefits:&lt;br /&gt;1. No repetition of class instantiation logic&lt;br /&gt;2. No repetition of ‘which class to invoke’ logic&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/186002073186625662-4741128178936535666?l=shreyas735.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shreyas735.blogspot.com/feeds/4741128178936535666/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=186002073186625662&amp;postID=4741128178936535666' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/4741128178936535666'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/4741128178936535666'/><link rel='alternate' type='text/html' href='http://shreyas735.blogspot.com/2008/07/factory-patterns.html' title='Factory Patterns'/><author><name>Parth</name><uri>http://www.blogger.com/profile/05955499680718949722</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-186002073186625662.post-8643790691120760009</id><published>2008-07-17T09:31:00.000-07:00</published><updated>2008-07-20T09:33:17.722-07:00</updated><title type='text'>Reflections</title><content type='html'>Reflections – Method&lt;br /&gt;Reflection is an important aspect of Java programming, with widespread usage in huge architectures.&lt;br /&gt;Reflections are a set of classes and interfaces provided with the the java.lang.reflect package. It allows programmatic access to information about fields, methods and constructors of loaded classes. In this blog we discuss about Method class that provides information pertaining to the methods of the loaded class, and allows invocation of a method, using the API’s. The steps we follow here are :&lt;br /&gt;1. Get handle to the correct method using getName() method.&lt;br /&gt;2. Invoke the method passing the object and the arguments.&lt;br /&gt;import java.lang.reflect.InvocationTargetException;&lt;br /&gt;import java.lang.reflect.Method;&lt;br /&gt;public class ReflectExample&lt;br /&gt;{&lt;br /&gt;private String message;&lt;br /&gt;public ReflectExample()&lt;br /&gt;{&lt;br /&gt;}&lt;br /&gt;public void setMessage(String message)&lt;br /&gt;{&lt;br /&gt;this.message = message;&lt;br /&gt;}&lt;br /&gt;public String getMessage()&lt;br /&gt;{&lt;br /&gt;return message;&lt;br /&gt;}&lt;br /&gt;public static void main(String args[])&lt;br /&gt;{&lt;br /&gt;ReflectExample loadedObject = new ReflectExample();&lt;br /&gt;Method[] methods = loadedObject.getClass().getMethods();&lt;br /&gt;for(int i = 0 ;i less methods.length;i++)&lt;br /&gt;{&lt;br /&gt;if(methods[i].getName().equals("setMessage"))&lt;br /&gt;{&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;methods[i].invoke(loadedObject, new Object[]{"This message has been set via reflections"});&lt;br /&gt;}&lt;br /&gt;catch (IllegalArgumentException e)&lt;br /&gt;{&lt;br /&gt;e.printStackTrace();&lt;br /&gt;}&lt;br /&gt;catch (IllegalAccessException e)&lt;br /&gt;{&lt;br /&gt;e.printStackTrace();&lt;br /&gt;}&lt;br /&gt;catch (InvocationTargetException e)&lt;br /&gt;{&lt;br /&gt;e.printStackTrace();&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;System.out.println(loadedObject.getMessage());&lt;br /&gt;}&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/186002073186625662-8643790691120760009?l=shreyas735.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shreyas735.blogspot.com/feeds/8643790691120760009/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=186002073186625662&amp;postID=8643790691120760009' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/8643790691120760009'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/8643790691120760009'/><link rel='alternate' type='text/html' href='http://shreyas735.blogspot.com/2008/07/reflections.html' title='Reflections'/><author><name>Parth</name><uri>http://www.blogger.com/profile/05955499680718949722</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-186002073186625662.post-5367584306270104864</id><published>2008-07-17T00:39:00.000-07:00</published><updated>2008-07-20T09:40:04.935-07:00</updated><title type='text'>Command Line Executor in Java</title><content type='html'>Have you ever wanted to open a notepad through your java code when running on Windows? Or for that matter have you ever wanted to get hold of the system command prompt and execute system specific processes? This blog explains how it can be achieved. &lt;br /&gt;&lt;br /&gt;Java exposes a class - Runtime that solves our problem. It has a method called "exec" that can be used to call any system specific commands. The method returns an instance of class Process that is the java representation of the process that gets initiated.&lt;br /&gt;&lt;br /&gt;Sample Code - CommandLine&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public class CommandLine &lt;br /&gt;{ &lt;br /&gt;&lt;br /&gt;public CommandLine() &lt;br /&gt;{ &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;public static void runCommand(String pCommands) &lt;br /&gt;{ &lt;br /&gt;Process objProcess = null; &lt;br /&gt;try &lt;br /&gt;{ &lt;br /&gt;objProcess = Runtime.getRuntime().exec(pCommands); &lt;br /&gt;} &lt;br /&gt;catch(Exception err) &lt;br /&gt;{ &lt;br /&gt;} &lt;br /&gt;} &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;To run a command the java code code would be something like this:&lt;br /&gt;&lt;br /&gt;CommandLine.runCommand("notepad.exe &lt;your file path&gt;");&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/186002073186625662-5367584306270104864?l=shreyas735.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shreyas735.blogspot.com/feeds/5367584306270104864/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=186002073186625662&amp;postID=5367584306270104864' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/5367584306270104864'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/5367584306270104864'/><link rel='alternate' type='text/html' href='http://shreyas735.blogspot.com/2008/07/command-line-executor-in-java.html' title='Command Line Executor in Java'/><author><name>Parth</name><uri>http://www.blogger.com/profile/05955499680718949722</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-186002073186625662.post-829913437806461781</id><published>2008-07-16T11:42:00.000-07:00</published><updated>2008-07-20T09:43:54.424-07:00</updated><title type='text'>Anonymous Inner classes</title><content type='html'>Anonymous Inner classes &lt;br /&gt;&lt;br /&gt;I have had a bad experience in understanding anonymous inner classes and that is one of the motivations in me deciding to post this blog. Let us try to understand anonymous inner classes literally first. Anonymous means ‘having no name’. Inner specifies that the class resides inside another class. So why do we need such a class? Firstly it reduces time and effort in writing another class that we might have to write in absence of this Java feature. Secondly some classes need to be present at many places but at each place the functionality demands are different, here the true powers of inner classes are revealed.(This will become evident once we see the example) Without wasting much time let us look at a real example.&lt;br /&gt;&lt;br /&gt;Action listeners for buttons are classes that implement ActionListener interface, and provide a method called ‘actionPerformed’, which is a callback each time a button is pressed. Now if the form you are working on contains a hell lot of buttons each performing menial tasks you don’t want to write a separate listener class for each of these. Neither do you want to add just one class for all buttons where you would be forced to write multiple if-else blocks to find out which button produced the event.&lt;br /&gt;&lt;br /&gt;In such a scenario, first create a class implementing ActionListener interface&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;import java.awt.event.ActionEvent; &lt;br /&gt;import java.awt.event.ActionListener; &lt;br /&gt;&lt;br /&gt;public class ActionListenerController implements ActionListener &lt;br /&gt;{ &lt;br /&gt;&lt;br /&gt;public void actionPerformed(ActionEvent event) &lt;br /&gt;{ &lt;br /&gt;//Override this method where used as an inner class. &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;Now to add an instance of this class to your buttons do the following. This same approach an be applied to all your buttons. &lt;br /&gt;button.addActionListener(new ActionListenerController() &lt;br /&gt;{ &lt;br /&gt;public void actionPerformed(ActionEvent event) &lt;br /&gt;{ &lt;br /&gt;//Write your code here &lt;br /&gt;} &lt;br /&gt;});&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/186002073186625662-829913437806461781?l=shreyas735.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shreyas735.blogspot.com/feeds/829913437806461781/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=186002073186625662&amp;postID=829913437806461781' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/829913437806461781'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/829913437806461781'/><link rel='alternate' type='text/html' href='http://shreyas735.blogspot.com/2008/07/anonymous-inner-classes.html' title='Anonymous Inner classes'/><author><name>Parth</name><uri>http://www.blogger.com/profile/05955499680718949722</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-186002073186625662.post-950787921109941167</id><published>2008-07-14T09:33:00.000-07:00</published><updated>2008-07-20T09:34:28.958-07:00</updated><title type='text'>Swings File chooser</title><content type='html'>File Chooser in Java Swings:&lt;br /&gt;&lt;br /&gt;Many Core Java single system applications require the user to choose a file/ folder as part of input to the utility built. Since this can be quite an irritating task if you let the user key in the path to the file or the folder, Java comes up with a JFileChooser class that lets the user browse the file system and choose using a visual interface. This is how you use it:&lt;br /&gt;&lt;br /&gt;Firstly add a button for the file browsing to start. Next to the listener add the following piece of code.&lt;br /&gt;&lt;br /&gt;JFileChooser chooser = new JFileChooser("");&lt;br /&gt;&lt;br /&gt;chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);&lt;br /&gt;&lt;br /&gt;//Sets the selection mode to files,can be changed to Folders&lt;br /&gt;&lt;br /&gt;int option = chooser.showOpenDialog(frame);&lt;br /&gt;&lt;br /&gt;//Opens the dialog for choosing file&lt;br /&gt;&lt;br /&gt;if (option == JFileChooser.APPROVE_OPTION)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;File dir = chooser.getSelectedFile();&lt;br /&gt;&lt;br /&gt;System.out.println(dir.getPath());&lt;br /&gt;&lt;br /&gt;//Prints the path selected by the user&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Thus you see it is very easy to add file browsing functionality to your Java program.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;An abstract class named FileFilter can be used to restrict user selection to specific file formats. In your chooser class you can set the file filter with an overridden ‘accept’ method that tells you whether the file is in the acceptable format. The file chooser dialog opens up with that file type as default. However it is a weak restriction and allows the user to select other file types and proceed. Any validations have to be handled explicitly.&lt;br /&gt;&lt;br /&gt;chooser.setFileFilter(new FileFilter()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public boolean accept(File f)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;return f.getName().toLowerCase().endsWith(".xls");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public String getDescription()&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;return ".xls files";&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;});&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/186002073186625662-950787921109941167?l=shreyas735.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shreyas735.blogspot.com/feeds/950787921109941167/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=186002073186625662&amp;postID=950787921109941167' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/950787921109941167'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/950787921109941167'/><link rel='alternate' type='text/html' href='http://shreyas735.blogspot.com/2008/07/swings-file-chooser.html' title='Swings File chooser'/><author><name>Parth</name><uri>http://www.blogger.com/profile/05955499680718949722</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-186002073186625662.post-7650241232616036857</id><published>2008-07-13T09:28:00.000-07:00</published><updated>2008-07-20T09:31:26.001-07:00</updated><title type='text'>File Transfer Protocol Utility</title><content type='html'>File Transfer Protocol&lt;br /&gt;FTP is a network protocol based on TCP, which facilitates movement of files over the network. In this blog I post a sample utility as an attachment that can be used for doing file transfers. This utility uses the apache commons net API’s to achieve it. I will briefly explain the methods that are part of the utility.&lt;br /&gt;The main FTP class we use here is FTPClient. It contains API’s to do a hell lot of things, which you will come across when you start inspecting the code. The main helper methods are:&lt;br /&gt;1. Upload – Transfer a local file to a remote Ftp server&lt;br /&gt;2. Download – Transfer a remote file to local machine.&lt;br /&gt;3. CreateRemoteDirectory – Create a new remote directory.&lt;br /&gt;4. DeleteRemoteFile – Delete a remote file.&lt;br /&gt;5. RemoteDirectoryExists – Returns true if remote directory exists&lt;br /&gt;6. RemoteFileExists – Returns true if remote file exists.&lt;br /&gt;Check out the util by clicking on the icon below:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;import java.io.FileInputStream;&lt;br /&gt;import java.io.FileOutputStream;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.io.InputStream;&lt;br /&gt;import org.apache.commons.net.ftp.FTP;&lt;br /&gt;import org.apache.commons.net.ftp.FTPClient;&lt;br /&gt;import org.apache.commons.net.ftp.FTPConnectionClosedException;&lt;br /&gt;import org.apache.commons.net.ftp.FTPFile;&lt;br /&gt;import org.apache.commons.net.ftp.FTPReply;&lt;br /&gt;public class FTPUtil&lt;br /&gt;{&lt;br /&gt;FTPClient ftp = null;&lt;br /&gt;private static String url;&lt;br /&gt;private static String user;&lt;br /&gt;private static String password;&lt;br /&gt;private static String remoteFileSeparator;&lt;br /&gt;private static String localFileSeparator;&lt;br /&gt;static&lt;br /&gt;{&lt;br /&gt;url = "&lt;&lt;&gt;&gt;";&lt;br /&gt;user = "&lt;&lt;&gt;&gt;";&lt;br /&gt;password = "&lt;&lt;&gt;&gt;";&lt;br /&gt;remoteFileSeparator = "&lt;&lt;&gt;&gt;";&lt;br /&gt;localFileSeparator = "&lt;&lt;&gt;&gt;";&lt;br /&gt;}&lt;br /&gt;public void Upload(String remoteFilePath, String localFilePath, String fileName) throws Exception&lt;br /&gt;{&lt;br /&gt;FTPClient ftp = new FTPClient();&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;ftp.connect(url);&lt;br /&gt;if (ftp.login(user, password))&lt;br /&gt;{&lt;br /&gt;if (!FTPReply.isPositiveCompletion(ftp.getReplyCode()))&lt;br /&gt;{&lt;br /&gt;ftp.disconnect();&lt;br /&gt;throw new Exception("FTP Login failed");&lt;br /&gt;}&lt;br /&gt;ftp.setFileType(FTP.BINARY_FILE_TYPE);&lt;br /&gt;InputStream input = new FileInputStream(localFilePath + localFileSeparator + fileName);&lt;br /&gt;boolean status = ftp.storeFile(remoteFilePath + remoteFileSeparator + fileName, input);&lt;br /&gt;if (status)&lt;br /&gt;{&lt;br /&gt;input.close();&lt;br /&gt;ftp.logout();&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;throw new Exception("Upload failed");&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;throw new Exception("FTP Connection failed");&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;catch (FTPConnectionClosedException ftpConnectionClosedException)&lt;br /&gt;{&lt;br /&gt;throw ftpConnectionClosedException;&lt;br /&gt;}&lt;br /&gt;catch (IOException ioException)&lt;br /&gt;{&lt;br /&gt;throw ioException;&lt;br /&gt;}&lt;br /&gt;finally&lt;br /&gt;{&lt;br /&gt;closeFTPConnection(ftp);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;public void Download(String remoteFilePath, String localFilePath, String filename) throws FTPConnectionClosedException, IOException, Exception&lt;br /&gt;{&lt;br /&gt;FTPClient ftp = new FTPClient();&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;ftp.connect(url);&lt;br /&gt;if (ftp.login(user, password))&lt;br /&gt;{&lt;br /&gt;if (!FTPReply.isPositiveCompletion(ftp.getReplyCode()))&lt;br /&gt;{&lt;br /&gt;ftp.disconnect();&lt;br /&gt;throw new Exception("FTP login failed");&lt;br /&gt;}&lt;br /&gt;FileOutputStream fileOutputStream = new FileOutputStream(localFilePath + localFileSeparator + filename);&lt;br /&gt;boolean status = ftp.retrieveFile(remoteFilePath, fileOutputStream);&lt;br /&gt;fileOutputStream.close();&lt;br /&gt;if (!status)&lt;br /&gt;{&lt;br /&gt;throw new Exception("Download failed");&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;throw new Exception("FTP Connection failed");&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;catch (FTPConnectionClosedException ftpConnectionClosedException)&lt;br /&gt;{&lt;br /&gt;throw ftpConnectionClosedException;&lt;br /&gt;}&lt;br /&gt;catch (IOException ioException)&lt;br /&gt;{&lt;br /&gt;throw ioException;&lt;br /&gt;}&lt;br /&gt;finally&lt;br /&gt;{&lt;br /&gt;closeFTPConnection(ftp);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;public static boolean RemoteFileExists(String remoteFilePath, String fileName) throws FTPConnectionClosedException, IOException, Exception&lt;br /&gt;{&lt;br /&gt;FTPClient ftp = new FTPClient();&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;ftp.connect(url);&lt;br /&gt;if (ftp.login(user, password))&lt;br /&gt;{&lt;br /&gt;if (!FTPReply.isPositiveCompletion(ftp.getReplyCode()))&lt;br /&gt;{&lt;br /&gt;ftp.disconnect();&lt;br /&gt;throw new Exception("FTP login failed");&lt;br /&gt;}&lt;br /&gt;FTPFile[] files = ftp.listFiles(remoteFilePath + remoteFileSeparator + fileName);&lt;br /&gt;if (files.length == 0)&lt;br /&gt;{&lt;br /&gt;return false;&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;return true;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;throw new Exception("Invalid login details");&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;catch (FTPConnectionClosedException ftpConnectionClosedException)&lt;br /&gt;{&lt;br /&gt;throw ftpConnectionClosedException;&lt;br /&gt;}&lt;br /&gt;catch (IOException ioException)&lt;br /&gt;{&lt;br /&gt;throw ioException;&lt;br /&gt;}&lt;br /&gt;finally&lt;br /&gt;{&lt;br /&gt;closeFTPConnection(ftp);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;public static boolean RemoteDirectoryExists(String remoteDirPath) throws FTPConnectionClosedException, IOException, Exception&lt;br /&gt;{&lt;br /&gt;FTPClient ftp = new FTPClient();&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;ftp.connect(url);&lt;br /&gt;if (ftp.login(user, password))&lt;br /&gt;{&lt;br /&gt;if (!FTPReply.isPositiveCompletion(ftp.getReplyCode()))&lt;br /&gt;{&lt;br /&gt;ftp.disconnect();&lt;br /&gt;throw new Exception("FTP login failed");&lt;br /&gt;}&lt;br /&gt;return (ftp.changeWorkingDirectory(remoteDirPath));&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;throw new Exception("Invalid login details");&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;catch (FTPConnectionClosedException ftpConnectionClosedException)&lt;br /&gt;{&lt;br /&gt;throw ftpConnectionClosedException;&lt;br /&gt;}&lt;br /&gt;catch (IOException ioException)&lt;br /&gt;{&lt;br /&gt;throw ioException;&lt;br /&gt;}&lt;br /&gt;finally&lt;br /&gt;{&lt;br /&gt;closeFTPConnection(ftp);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;public void DeleteRemoteFile(String path, String filename) throws FTPConnectionClosedException, Exception&lt;br /&gt;{&lt;br /&gt;FTPClient ftp = new FTPClient();&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;ftp.connect(url);&lt;br /&gt;if (ftp.login(user, password))&lt;br /&gt;{&lt;br /&gt;if (!FTPReply.isPositiveCompletion(ftp.getReplyCode()))&lt;br /&gt;{&lt;br /&gt;ftp.disconnect();&lt;br /&gt;throw new Exception("FTP Login failed");&lt;br /&gt;}&lt;br /&gt;if (!ftp.deleteFile(path + filename))&lt;br /&gt;{&lt;br /&gt;throw new Exception("Delete failed");&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;throw new Exception("Invalid login details");&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;catch (FTPConnectionClosedException ftpConnectionClosedException)&lt;br /&gt;{&lt;br /&gt;throw ftpConnectionClosedException;&lt;br /&gt;}&lt;br /&gt;finally&lt;br /&gt;{&lt;br /&gt;closeFTPConnection(ftp);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;public void CreateRemoteDirectory(String path) throws FTPConnectionClosedException, Exception&lt;br /&gt;{&lt;br /&gt;FTPClient ftp = new FTPClient();&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;ftp.connect(url);&lt;br /&gt;if (ftp.login(user, password))&lt;br /&gt;{&lt;br /&gt;if (!FTPReply.isPositiveCompletion(ftp.getReplyCode()))&lt;br /&gt;{&lt;br /&gt;ftp.disconnect();&lt;br /&gt;throw new Exception("FTP login failed");&lt;br /&gt;}&lt;br /&gt;ftp.mkd(path);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;throw new Exception("Invalid login details");&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;catch (FTPConnectionClosedException ftpConnectionClosedException)&lt;br /&gt;{&lt;br /&gt;throw ftpConnectionClosedException;&lt;br /&gt;}&lt;br /&gt;finally&lt;br /&gt;{&lt;br /&gt;closeFTPConnection(ftp);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;private static void closeFTPConnection(FTPClient ftp)&lt;br /&gt;{&lt;br /&gt;if (ftp.isConnected())&lt;br /&gt;{&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;ftp.disconnect();&lt;br /&gt;}&lt;br /&gt;catch (IOException e1)&lt;br /&gt;{&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/186002073186625662-7650241232616036857?l=shreyas735.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shreyas735.blogspot.com/feeds/7650241232616036857/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=186002073186625662&amp;postID=7650241232616036857' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/7650241232616036857'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/7650241232616036857'/><link rel='alternate' type='text/html' href='http://shreyas735.blogspot.com/2008/07/file-transfer-protocol-utility.html' title='File Transfer Protocol Utility'/><author><name>Parth</name><uri>http://www.blogger.com/profile/05955499680718949722</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-186002073186625662.post-3564503142309749895</id><published>2008-07-13T00:46:00.000-07:00</published><updated>2008-07-20T09:46:58.492-07:00</updated><title type='text'>XSD Validation</title><content type='html'>It has only been a day since my first info blog, and I just cannot wait to write a new one. Have you ever faced difficulties in implementing a validation for you xml document? There are precisely two ways of doing XML validation.&lt;br /&gt;&lt;br /&gt;First of these is using a DTD document. A hell lot of effort goes in writing dtd and even if you manage to write it, it is not easy to comprehend.&lt;br /&gt;&lt;br /&gt;An easier way of achieving the same is using XSD documents. Benefits are quite evident, if you get the hang of it. Here in this info blog I share with you a snippet that demonstrates how it is done.&lt;br /&gt;&lt;br /&gt;XML validation function:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;private boolean IsXmlValid() &lt;br /&gt;{ &lt;br /&gt;try &lt;br /&gt;{ &lt;br /&gt;SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); &lt;br /&gt;File schemaLocation = new File(xsdPath); &lt;br /&gt;Schema schema = factory.newSchema(schemaLocation); &lt;br /&gt;Validator validator = schema.newValidator(); &lt;br /&gt;DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); &lt;br /&gt;domFactory.setNamespaceAware(true); &lt;br /&gt;DocumentBuilder builder = domFactory.newDocumentBuilder(); &lt;br /&gt;Document doc = builder.parse(new File(xmlPath)); &lt;br /&gt;DOMSource source = new DOMSource(doc); &lt;br /&gt;DOMResult result = new DOMResult(); &lt;br /&gt;validator.validate(source, result); &lt;br /&gt;return true; &lt;br /&gt;} &lt;br /&gt;catch (SAXException ex) &lt;br /&gt;{ &lt;br /&gt;return false; &lt;br /&gt;} &lt;br /&gt;catch (Exception ex) &lt;br /&gt;{ &lt;br /&gt;return false; &lt;br /&gt;} &lt;br /&gt;} &lt;br /&gt;} &lt;br /&gt;The method returns true if the XML complies with the XSD. The xsdpath and xmlPath have to be replaced with actual files paths.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/186002073186625662-3564503142309749895?l=shreyas735.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shreyas735.blogspot.com/feeds/3564503142309749895/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=186002073186625662&amp;postID=3564503142309749895' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/3564503142309749895'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/3564503142309749895'/><link rel='alternate' type='text/html' href='http://shreyas735.blogspot.com/2008/07/xsd-validation.html' title='XSD Validation'/><author><name>Parth</name><uri>http://www.blogger.com/profile/05955499680718949722</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-186002073186625662.post-1557692011793988302</id><published>2008-07-09T09:25:00.000-07:00</published><updated>2008-07-20T09:26:55.120-07:00</updated><title type='text'>Connection using DriverManager</title><content type='html'>Let us start with a little basics about database connections in Java. The first question that might intrigue someone is why does Java have Connection as an interface rather than a class. This is because different databases have different implementations of the Connection class, ie when you get a connection object for an Oracle database the implementing class is oracle.jdbc.driver.OracleConnection. Some other database would have some other class that implements the java.sql.Connection interface. Thus each database company comes up with its own database driver that will provide the implementation of Connection as a full fledged class.&lt;br /&gt;DriverManager is the basic service for managing a set of JDBC drivers. When referenced the DriverManager loads the driver classes referenced in the jdbc.drivers system property. However this is hardly ever used. The application that you are developing loads a driver class explicitly before trying to obtain a Connection. This is generally done by using Class.forName() method.&lt;br /&gt;When the method getConnection is called, the DriverManager will attempt to locate a suitable driver from amongst those loaded at initialization and those loaded explicitly using the same classloader as the current applet or application.&lt;br /&gt;The following code snippet explains the procedure to obtain a database connection using Drivermanager class:&lt;br /&gt;&lt;br /&gt;public static Connection getConnection() throws ClassNotFoundException, SQLException&lt;br /&gt;{&lt;br /&gt;Connection conn = null;&lt;br /&gt;String ip = "enter the ip address of database server";&lt;br /&gt;String port = "enter the port at which db server is listening";&lt;br /&gt;String sid = "enter the datasource name as in tnsnames.ora dsn";&lt;br /&gt;String uname = "enter db user";&lt;br /&gt;String pwd = "enter the db password";&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;Class.forName("oracle.jdbc.driver.OracleDriver");&lt;br /&gt;//Loads the Oracle driver.&lt;br /&gt;//The DriverManager will now start using this Driver to fetch implementation of Connection&lt;br /&gt;String url = "jdbc:oracle:thin:@" + ip + ":" + port + ":" + sid;&lt;br /&gt;//Create the URL for an Oracle database&lt;br /&gt;conn = DriverManager.getConnection(url, uname, pwd);&lt;br /&gt;//DriverManager uses oracle.jdbc.driver.OracleDriver to return oracle.jdbc.driver.OracleConnection&lt;br /&gt;return conn;&lt;br /&gt;}&lt;br /&gt;catch (ClassNotFoundException e1)&lt;br /&gt;{&lt;br /&gt;throw e1;&lt;br /&gt;}&lt;br /&gt;catch (SQLException e2)&lt;br /&gt;{&lt;br /&gt;throw e2;&lt;br /&gt;}&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/186002073186625662-1557692011793988302?l=shreyas735.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shreyas735.blogspot.com/feeds/1557692011793988302/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=186002073186625662&amp;postID=1557692011793988302' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/1557692011793988302'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/1557692011793988302'/><link rel='alternate' type='text/html' href='http://shreyas735.blogspot.com/2008/07/connection-using-drivermanager.html' title='Connection using DriverManager'/><author><name>Parth</name><uri>http://www.blogger.com/profile/05955499680718949722</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-186002073186625662.post-315134560678843115</id><published>2008-07-08T16:34:00.000-07:00</published><updated>2008-07-20T09:36:07.150-07:00</updated><title type='text'>Reading Excel POI</title><content type='html'>Reading Microsoft Excel 97 files through a Java program using Apache POI HSSF project:&lt;br /&gt;&lt;br /&gt;Apache POI project is centered on providing API’s for easy read and write capabilities over Microsoft format files. For reading excel files, there is a HSSF project which currently supports only Excel’97. That is it does not support the .xlsx files. In this blog I will explain the methodology to be used for reading .xls files through this Java API.&lt;br /&gt;&lt;br /&gt;The first step is to initialize an object named POIFSFileSystem. This can be done as follows:&lt;br /&gt;&lt;br /&gt;String excelFilePath = “C:\\Temp\a.xls”;&lt;br /&gt;&lt;br /&gt;FileInputStream file = new FileInputStream(excelFilePath);&lt;br /&gt;&lt;br /&gt;POIFSFileSystem poifsFileSystem = new POIFSFileSystem(file);&lt;br /&gt;&lt;br /&gt;Secondly you initialize two more objects: HSSFWorkbook and HSSFSheet&lt;br /&gt;&lt;br /&gt;HSSFWorkbook hssfWorkBook = new HSSFWorkbook(poifsFileSystem);&lt;br /&gt;&lt;br /&gt;HSSFSheet hssfSheet = hssfWorkBook.getSheetAt(0); &lt;br /&gt;&lt;br /&gt;The getSheetAt(index) method specifies which sheet index you want to be read. The index for the first sheet in an Excel sheet is 0. Now suppose you want to iterate over the rows of the Excel sheet. This requires another object named HSSFRow. Iteration can be achieved using the following code:&lt;br /&gt;&lt;br /&gt;HSSFRow hssfRow = hssfSheet.getRow(1);&lt;br /&gt;&lt;br /&gt;int count = 1;&lt;br /&gt;&lt;br /&gt;while (hssfRow != null)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;hssfRow = hssfSheet.getRow(count++);&lt;br /&gt;&lt;br /&gt;if (hssfRow != null)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;System.out.println(hssfRow.getCell((short) 0).getStringCellValue());&lt;br /&gt;&lt;br /&gt;//first column String printed&lt;br /&gt;&lt;br /&gt;System.out.println(hssfRow.getCell((short) 1). getNumericCellValue());&lt;br /&gt;&lt;br /&gt;//Numeric second column printed &lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Thus you can see POI gives you an extremely structured way of accessing Excel files. More about POI can be found at http://poi.apache.org/index.html.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/186002073186625662-315134560678843115?l=shreyas735.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shreyas735.blogspot.com/feeds/315134560678843115/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=186002073186625662&amp;postID=315134560678843115' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/315134560678843115'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/315134560678843115'/><link rel='alternate' type='text/html' href='http://shreyas735.blogspot.com/2008/07/reading-excel-poi.html' title='Reading Excel POI'/><author><name>Parth</name><uri>http://www.blogger.com/profile/05955499680718949722</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-186002073186625662.post-1654950429677789393</id><published>2008-07-07T10:18:00.000-07:00</published><updated>2008-07-20T09:25:37.361-07:00</updated><title type='text'>Timers in Java</title><content type='html'>Timer and TimerTask Classes&lt;br /&gt;A task that needs to be scheduled to happen after a certain time interval, or an action that needs to be performed periodically, can be achieved using java.util.Timer and java.util.TimerTask classes in Java.&lt;br /&gt;Timer and TimerTask have been provided as helpers to Threads.&lt;br /&gt;TimerTask is essentially an abstract class that has a method called run(). To initialize this class all one has to do is provide an implementation of this method either inside a class extending TimerTask or inside an anonymous inner class block. The method run() should contain all the logic that is expected to run periodically.&lt;br /&gt;No comes the Timer which contains methods to schedule the timer task.The most often used method is&lt;br /&gt;A. Fixed Delay&lt;br /&gt;schedule(TimerTask task, long delay, long period)&lt;br /&gt;Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.&lt;br /&gt;The delay argument takes as input time in milliseconds after which the first call to the task’s run method would be made.&lt;br /&gt;The period argument takes as input the time lapse, in milliseconds, after the execution of the previous execution, for which the task should wait before making another call. That is the timer task would get called after fixed delays.&lt;br /&gt;B. Fixed Rate&lt;br /&gt;scheduleAtFixedRate(TimerTask task, long delay, long period)&lt;br /&gt;This method schedules the execution of the timer task such that irrespective of how much time it takes to execute a previous call; fresh calls to the task must be made at a fixed rate. That is the timer task would get called at a fixed rate.&lt;br /&gt;Code Fragment:&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;int delay = 1000; // delay for 1 sec.&lt;br /&gt;int period = 5000; // repeat every 5 sec.&lt;br /&gt;Timer timer = new Timer();&lt;br /&gt;timer.scheduleAtFixedRate(new TimerTask()&lt;br /&gt;{&lt;br /&gt;public void run()&lt;br /&gt;{&lt;br /&gt;// Add the action code here&lt;br /&gt;}&lt;br /&gt;}, delay, period);&lt;br /&gt;}&lt;br /&gt;catch (Exception e)&lt;br /&gt;{&lt;br /&gt;e.printStackTrace();&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/186002073186625662-1654950429677789393?l=shreyas735.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shreyas735.blogspot.com/feeds/1654950429677789393/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=186002073186625662&amp;postID=1654950429677789393' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/1654950429677789393'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/1654950429677789393'/><link rel='alternate' type='text/html' href='http://shreyas735.blogspot.com/2008/07/timers-in-java.html' title='Timers in Java'/><author><name>Parth</name><uri>http://www.blogger.com/profile/05955499680718949722</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-186002073186625662.post-6264070499644095183</id><published>2008-07-05T14:16:00.000-07:00</published><updated>2008-07-20T09:24:44.947-07:00</updated><title type='text'>Unzip a file using Java API's</title><content type='html'>UnZipping a Zip file using Java APIS.&lt;br /&gt;Java provides support for WinZip. To achieve this Java comes up with two utility classes called ZipEntry and ZipFile. Achieving unzip functionality is simple and does not require more than 5-6 lines of code. Though copying files using Streams can take up time.&lt;br /&gt;First thing that needs to be done here is instantiation of ZipFile. To the contructor all you need to pass is the system path to the zip file you wish to unzip.&lt;br /&gt;eg. new ZipFile(strFilePath + "\\" + strFileName);&lt;br /&gt;Now this class exposes a method called "entries()" that returns an enumeration of ZipEntry classes. Simple iteration logic over this enumeration type gets us handles to each and every file there is inside the zip file.&lt;br /&gt;The next step is initializing input and output streams. The logic to get the output stream is simple. Just create a directory using mkdirs() method of File class and initialize an FileOutputStream for that location.&lt;br /&gt;The step for getting the input stream is a bit complicated:&lt;br /&gt;objInputStream = objZipFile.getInputStream(objZipEntry);&lt;br /&gt;Here onbjZipEntry is the current Zip entry that is getting processed out of the enumeration.&lt;br /&gt;Now simply copy the file from the input to the output stream&lt;br /&gt;Given below is the source file telling you how to achieve this. Check and modify the main as you wish.&lt;br /&gt;&lt;br /&gt;import java.io.File;&lt;br /&gt;import java.io.FileNotFoundException;&lt;br /&gt;import java.io.FileOutputStream;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.io.InputStream;&lt;br /&gt;import java.util.Enumeration;&lt;br /&gt;import java.util.zip.ZipEntry;&lt;br /&gt;import java.util.zip.ZipFile;&lt;br /&gt;public class ZipUtil&lt;br /&gt;{&lt;br /&gt;public static void doProcessZip(String strFilePath, String strFileName)&lt;br /&gt;{&lt;br /&gt;FileOutputStream objFileOutputStream = null;&lt;br /&gt;InputStream objInputStream = null;&lt;br /&gt;ZipEntry objZipEntry = null;&lt;br /&gt;Enumeration objEnumeration = null;&lt;br /&gt;ZipFile objZipFile = null;&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;objZipFile = new ZipFile(strFilePath + "\\" + strFileName);&lt;br /&gt;for (objEnumeration = objZipFile.entries(); objEnumeration.hasMoreElements();)&lt;br /&gt;{&lt;br /&gt;objZipEntry = (ZipEntry) objEnumeration.nextElement();&lt;br /&gt;if (objZipEntry.getName().indexOf("/") &gt; 0)&lt;br /&gt;{&lt;br /&gt;new File(strFilePath + "\\" + strFileName.substring(0, strFileName.indexOf('.')) + "\\" + objZipEntry.getName().substring(0, objZipEntry.getName().indexOf("/"))).mkdirs();&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;(new File(strFilePath + "\\" + strFileName.substring(0, strFileName.indexOf('.')))).mkdir();&lt;br /&gt;}&lt;br /&gt;objFileOutputStream = new FileOutputStream(strFilePath + "\\" + strFileName.substring(0, strFileName.indexOf('.')) + "\\" + objZipEntry.getName());&lt;br /&gt;objInputStream = objZipFile.getInputStream(objZipEntry);&lt;br /&gt;copyInputStream(objInputStream, objFileOutputStream);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;catch (FileNotFoundException e)&lt;br /&gt;{&lt;br /&gt;e.printStackTrace();&lt;br /&gt;}&lt;br /&gt;catch (IOException e)&lt;br /&gt;{&lt;br /&gt;e.printStackTrace();&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;public static final void copyInputStream(InputStream in, FileOutputStream fout) throws IOException&lt;br /&gt;{&lt;br /&gt;byte buffer[] = new byte[1024];&lt;br /&gt;int len;&lt;br /&gt;while ((len = in.read(buffer)) &gt;= 0)&lt;br /&gt;{&lt;br /&gt;fout.write(buffer, 0, len);&lt;br /&gt;}&lt;br /&gt;in.close();&lt;br /&gt;fout.close();&lt;br /&gt;}&lt;br /&gt;public static void main(String args[])&lt;br /&gt;{&lt;br /&gt;doProcessZip("c:\\", "abc.zip");&lt;br /&gt;}&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/186002073186625662-6264070499644095183?l=shreyas735.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shreyas735.blogspot.com/feeds/6264070499644095183/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=186002073186625662&amp;postID=6264070499644095183' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/6264070499644095183'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/6264070499644095183'/><link rel='alternate' type='text/html' href='http://shreyas735.blogspot.com/2008/07/unzip-file-using-java-apis.html' title='Unzip a file using Java API&apos;s'/><author><name>Parth</name><uri>http://www.blogger.com/profile/05955499680718949722</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-186002073186625662.post-1793338998756806387</id><published>2008-07-04T11:40:00.000-07:00</published><updated>2008-07-20T09:42:39.614-07:00</updated><title type='text'>XSL Transforms</title><content type='html'>This blog explains java implementation of XSL transformations. Let us try and understand a few basic things about XSL transforms. XSL is the standard used to add rendering information to xml documents. XML contains raw data that has to be rendered to the user in various formats. XSL files contain the logic required to read the xml file and transform it to the required format. This transformation is generally to html, so that data can be presented to the user in a format that makes more sense to him. Thus we would need two files an xml document, and a corresponding XSL document. The XSL would read the XML and transform it to html which would be streamed out to a result.html file.&lt;br /&gt;&lt;br /&gt;  &lt;br /&gt;import java.io.File; &lt;br /&gt;import javax.xml.transform.Result;&lt;br /&gt;import javax.xml.transform.Source;&lt;br /&gt;import javax.xml.transform.Transformer;&lt;br /&gt;import javax.xml.transform.TransformerConfigurationException;&lt;br /&gt;import javax.xml.transform.TransformerException;&lt;br /&gt;import javax.xml.transform.TransformerFactory;&lt;br /&gt;import javax.xml.transform.TransformerFactoryConfigurationError;&lt;br /&gt;import javax.xml.transform.stream.StreamResult;&lt;br /&gt;import javax.xml.transform.stream.StreamSource;&lt;br /&gt;public class XSLTransformer&lt;br /&gt;{&lt;br /&gt;public static void main(String[] args)&lt;br /&gt;{&lt;br /&gt;Source xmlSource = new StreamSource(new File("transform.xml"));&lt;br /&gt;Source xslSource = new StreamSource(new File("transform.xsl"));&lt;br /&gt;Result htmlResult = new StreamResult(new File("result.html"));&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;Transformer transformer = TransformerFactory.newInstance().newTransformer(xslSource);&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;transformer.transform(xmlSource, htmlResult);&lt;br /&gt;}&lt;br /&gt;catch (TransformerException e)&lt;br /&gt;{&lt;br /&gt;e.printStackTrace();&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;catch (TransformerConfigurationException e)&lt;br /&gt;{&lt;br /&gt;e.printStackTrace();&lt;br /&gt;}&lt;br /&gt;catch (TransformerFactoryConfigurationError e)&lt;br /&gt;{&lt;br /&gt;e.printStackTrace();&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/186002073186625662-1793338998756806387?l=shreyas735.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shreyas735.blogspot.com/feeds/1793338998756806387/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=186002073186625662&amp;postID=1793338998756806387' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/1793338998756806387'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/1793338998756806387'/><link rel='alternate' type='text/html' href='http://shreyas735.blogspot.com/2008/07/xsl-transforms.html' title='XSL Transforms'/><author><name>Parth</name><uri>http://www.blogger.com/profile/05955499680718949722</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-186002073186625662.post-9132044913801114796</id><published>2008-07-02T20:10:00.000-07:00</published><updated>2008-07-20T09:23:50.882-07:00</updated><title type='text'>Java - Exception Handling</title><content type='html'>Exception handling&lt;br /&gt;Exceptions are unexpected occurrences/execution time errors in a program that denotes a situation where the Java runtime needs assistance from the programmer about what to be doing next. When you program for an Exception, in other words, when you write Exception handling code, you are doing just that. Whenever your program causes an error, Java throws an exception. Exception has been treated just like another object in Java, and this object carries information about the exception that occurred.&lt;br /&gt;JRE looks for a program block that is waiting to catch an Exception.&lt;br /&gt;It follows the following sequence while doing the same:&lt;br /&gt;1. If the statement that caused the error is lying inside a try – catch block, the control gets passed to that part of the code. After catch, the execution continues outside of the try-catch block.&lt;br /&gt;2. If the statement is without a try - catch block, the JRE looks upwards of the call stack trace to find out a try block that surrounds the call and the control gets passed to the catch block of that try. The execution then continues after that try-catch block. If suppose there are no try blocks surrounding the call, the exception is caught by the main thread exception handler that prints the exception and terminates the program.&lt;br /&gt;When to write exception handlers&lt;br /&gt;1. Checked Exceptions: These are usually errors in the input data. The programmer has no control over the input the user gives you, for example FileNotFoundException when the file path is not a valid path. Such exceptions are to be handled, java compiler forces you to write exception handlers for these types of exceptions and the programmer is supposed to decide the future course of the program in the catch block.&lt;br /&gt;When not to write exception handlers&lt;br /&gt;1. Programmatic errors: Unchecked Exceptions like NullPointerException, ArrayIndexOutofBoundsException can generally be avoided by performing null checks or size of array checks before making calls to methods that can give rise to these errors. Handling these exceptions is not desirable because if these exceptions do occur it means there is a problem with the code and the code needs fixing rather than exception handling.&lt;br /&gt;When to eat Exceptions&lt;br /&gt;1. Eating exceptions refers to ignoring of an exception in a catch block and allowing the program to execute further. These are occurrences where you do not want the execution to stop. i.e. though there has been an error, it is not big enough to stall the program execution.&lt;br /&gt;When to throw Exceptions&lt;br /&gt;1. Exception handling generally is cascaded across various Java classes, ie an exception that occurs at a class would impact a whole hierarchy of calls. This is generally the case when handling transactions. In such cases the exception must be caught at the very start of the call trace. Thus all exception handling (for exceptions that must stall the transaction) in all intermediate classes does only one thing, and that is to throw the exception to the previous calling block.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/186002073186625662-9132044913801114796?l=shreyas735.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shreyas735.blogspot.com/feeds/9132044913801114796/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=186002073186625662&amp;postID=9132044913801114796' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/9132044913801114796'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/9132044913801114796'/><link rel='alternate' type='text/html' href='http://shreyas735.blogspot.com/2008/07/java-exception-handling.html' title='Java - Exception Handling'/><author><name>Parth</name><uri>http://www.blogger.com/profile/05955499680718949722</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-186002073186625662.post-233895381712634657</id><published>2008-07-02T16:45:00.000-07:00</published><updated>2008-07-20T09:46:09.501-07:00</updated><title type='text'>Singleton Patterns</title><content type='html'>Since this is the first blog ever on this domain, let us start with something simple about Design patterns in Java. People having some acquaintance with design patterns would understand why I say this? So let's dive in to the simplest design patterns of all - Singletons&lt;br /&gt;&lt;br /&gt;When are they used? Singletons are classes that are usually meant to act as system level variables. What do I mean? Since there is no concept of System variables in Java, it happens quite often that there are unwanted variables across Java classes that store the same content. Singletons are classes that will get instantiated only once, that is only one instance would be created for the entire program life cycle. And that single instance will have all the content you need every now and then. Such content is most of the times read from a .properties file. Thus the example used here would be based on that.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;import java.io.*; &lt;br /&gt;import java.util.*; &lt;br /&gt;public class PropertiesUtil &lt;br /&gt;{ &lt;br /&gt;private static HashMap mapEntries = new HashMap(); &lt;br /&gt;private static PropertiesUtil propertiesUtil; &lt;br /&gt;protected PropertiesUtil() &lt;br /&gt;{ &lt;br /&gt;initialize(); &lt;br /&gt;} &lt;br /&gt;public static PropertiesUtil getInstance() &lt;br /&gt;{ &lt;br /&gt;if (propertiesUtil != null) &lt;br /&gt;{ &lt;br /&gt;return propertiesUtil; &lt;br /&gt;} &lt;br /&gt;else &lt;br /&gt;{ &lt;br /&gt;propertiesUtil = new PropertiesUtil(); &lt;br /&gt;return propertiesUtil; &lt;br /&gt;} &lt;br /&gt;} &lt;br /&gt;private void initialize() &lt;br /&gt;{ &lt;br /&gt;InputStream propertiesFile = null; &lt;br /&gt;Properties properties = new Properties(); &lt;br /&gt;Iterator keysIterator = null; &lt;br /&gt;String key = null; &lt;br /&gt;try &lt;br /&gt;{ &lt;br /&gt;propertiesFile = new FileInputStream(".//application.properties"); &lt;br /&gt;properties.load(propertiesFile); &lt;br /&gt;propertiesFile.close(); &lt;br /&gt;} &lt;br /&gt;catch (IOException ioe) &lt;br /&gt;{ &lt;br /&gt;} &lt;br /&gt;keysIterator = properties.keySet().iterator(); &lt;br /&gt;while (keysIterator.hasNext()) &lt;br /&gt;{ &lt;br /&gt;key = keysIterator.next().toString(); &lt;br /&gt;mapEntries.put(key, properties.get(key)); &lt;br /&gt;} &lt;br /&gt;} &lt;br /&gt;public String getProperty(String key, String defaultValue) &lt;br /&gt;{ &lt;br /&gt;if (mapEntries.get(key) != null) &lt;br /&gt;{ &lt;br /&gt;return mapEntries.get(key).toString().trim(); &lt;br /&gt;} &lt;br /&gt;else &lt;br /&gt;{ &lt;br /&gt;return defaultValue; &lt;br /&gt;} &lt;br /&gt;} &lt;br /&gt;} &lt;br /&gt;Should anyone need, would explain the working of the code in future posts.&lt;br /&gt;&lt;br /&gt;BFN!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/186002073186625662-233895381712634657?l=shreyas735.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shreyas735.blogspot.com/feeds/233895381712634657/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=186002073186625662&amp;postID=233895381712634657' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/233895381712634657'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/233895381712634657'/><link rel='alternate' type='text/html' href='http://shreyas735.blogspot.com/2008/07/singleton-patterns.html' title='Singleton Patterns'/><author><name>Parth</name><uri>http://www.blogger.com/profile/05955499680718949722</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-186002073186625662.post-1093928021922321921</id><published>2008-07-02T11:31:00.000-07:00</published><updated>2008-07-20T09:22:11.260-07:00</updated><title type='text'>Copying a file in Java without using Streams</title><content type='html'>Copying a file from one location to another is a hectic while loop if you are using Streams. The guys from Java have come up with a solution using FileChannel class. This article explains how to use the FileChannel class to copy a file. First of all we need two FileChannel objects one for the original file and another for the destination file. The class has a transferTo() method that can be used to accomplish what we desire. It takes three arguements, the first one specifying the start position of copy in long , the second one giving the size of input file and the third specifying the output channel. click on the link below to see the actual code fragment:&lt;br /&gt;&lt;br /&gt;public static void copyFile(File in, File out) throws IOException&lt;br /&gt;{&lt;br /&gt;FileChannel inChannel = new FileInputStream(in).getChannel();&lt;br /&gt;FileChannel outChannel = new FileOutputStream(out).getChannel();&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;inChannel.transferTo(0, inChannel.size(), outChannel);&lt;br /&gt;}&lt;br /&gt;catch (IOException e)&lt;br /&gt;{&lt;br /&gt;throw e;&lt;br /&gt;}&lt;br /&gt;finally&lt;br /&gt;{&lt;br /&gt;if (inChannel != null)&lt;br /&gt;inChannel.close();&lt;br /&gt;if (outChannel != null)&lt;br /&gt;outChannel.close();&lt;br /&gt;}&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/186002073186625662-1093928021922321921?l=shreyas735.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shreyas735.blogspot.com/feeds/1093928021922321921/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=186002073186625662&amp;postID=1093928021922321921' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/1093928021922321921'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/1093928021922321921'/><link rel='alternate' type='text/html' href='http://shreyas735.blogspot.com/2008/07/copying-file-in-java-without-using.html' title='Copying a file in Java without using Streams'/><author><name>Parth</name><uri>http://www.blogger.com/profile/05955499680718949722</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-186002073186625662.post-7054818606225307640</id><published>2008-07-02T09:36:00.000-07:00</published><updated>2008-07-20T09:37:06.922-07:00</updated><title type='text'>Secure Shell - Unix Java</title><content type='html'>Secure Shell – Linux/Unix&lt;br /&gt;&lt;br /&gt;Secure Shell or SSH is a network protocol that allows data to be exchanged using a secure channel between two computers. Encryption provides confidentiality and integrity of data over an insecure network, such as the Internet. SSH uses public-key cryptography to authenticate the remote computer and allow the remote computer to authenticate the user, if necessary.&lt;br /&gt;&lt;br /&gt;SSH is typically used to log into a remote machine and execute commands, but it also supports tunneling, forwarding arbitrary TCP ports and X11 connections; it can transfer files using the associated SFTP or SCP protocols.SSH uses the client-server protocol.&lt;br /&gt;&lt;br /&gt;An SSH server, by default, listens on the standard TCP port 22.&lt;br /&gt;&lt;br /&gt;An SSH client program is typically used for establishing connections to an SSH daemon accepting remote connections. Both are commonly present on most modern operating systems, including Mac OS X, Linux, FreeBSD, Solaris and OpenVMS. Proprietary, freeware and open source versions of various levels of complexity and completeness exist.&lt;br /&gt;&lt;br /&gt;Here in this blog I want to share the Java methodology of connecting to a UNIX/LINUX box and executing remote commands on the remote machine. The third party jar we use in this example is jsch-0.1.38.jar. &lt;br /&gt;&lt;br /&gt;The following method takes in the directory on the remote machine from where you want the command to execute. It takes in the command and the argument you wish to pass with the command.&lt;br /&gt;&lt;br /&gt;public static void rexec(String dir, String command, String arg) throws Exception&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;JSch jsch = new JSch();&lt;br /&gt;&lt;br /&gt;String output = null;&lt;br /&gt;&lt;br /&gt;Session session = null;&lt;br /&gt;&lt;br /&gt;Channel channel = null;&lt;br /&gt;&lt;br /&gt;InputStream in = null;&lt;br /&gt;&lt;br /&gt;int port = 22;&lt;br /&gt;&lt;br /&gt;byte tmp[] = new byte[1024];&lt;br /&gt;&lt;br /&gt;try&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;session = jsch.getSession(user, server, port);&lt;br /&gt;&lt;br /&gt;session.setPassword(password);&lt;br /&gt;&lt;br /&gt;java.util.Properties config = new java.util.Properties();&lt;br /&gt;&lt;br /&gt;config.put("StrictHostKeyChecking", "no");&lt;br /&gt;&lt;br /&gt;session.setConfig(config);&lt;br /&gt;&lt;br /&gt;session.connect();&lt;br /&gt;&lt;br /&gt;channel = session.openChannel("exec");&lt;br /&gt;&lt;br /&gt;((ChannelExec) channel).setCommand("cd " + dir + ";" + command + " " + arg);&lt;br /&gt;&lt;br /&gt;channel.setInputStream(null);&lt;br /&gt;&lt;br /&gt;((ChannelExec) channel).setErrStream(System.err);&lt;br /&gt;&lt;br /&gt;in = channel.getInputStream();&lt;br /&gt;&lt;br /&gt;channel.connect();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;catch (Exception e)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;throw e;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/186002073186625662-7054818606225307640?l=shreyas735.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shreyas735.blogspot.com/feeds/7054818606225307640/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=186002073186625662&amp;postID=7054818606225307640' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/7054818606225307640'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/7054818606225307640'/><link rel='alternate' type='text/html' href='http://shreyas735.blogspot.com/2008/07/secure-shell-unix-java.html' title='Secure Shell - Unix Java'/><author><name>Parth</name><uri>http://www.blogger.com/profile/05955499680718949722</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-186002073186625662.post-2174926912010055714</id><published>2008-07-01T16:38:00.000-07:00</published><updated>2008-07-20T09:38:58.451-07:00</updated><title type='text'>Abstract Factory Patterns</title><content type='html'>Factory patterns&lt;br /&gt;&lt;br /&gt;Anyone having little acquaintance with Java would have at some point in time used factories to obtain objects that would provide them with utility methods. The use of Factory patterns is quite simple. It helps the developer delegate the responsibility of creation of the right object at runtime to a separate class. Thus the main factory class always has a method that returns objects of type Base class. To give an idea, let there be a class named ‘Feature’ and several subclasses of class ‘Feature’. Now it may be required to get handles to subclass objects, without having to know the actual class that should be initialized at runtime. A Factory can come as a help, where in a method that takes an argument would create the desired class and return the object to the calling method.&lt;br /&gt;&lt;br /&gt;Rather than creating a working example for this pattern, we will see an existing Factory pattern in Java.&lt;br /&gt;&lt;br /&gt;The example that we use here is DocumentBuilderFactory.&lt;br /&gt;&lt;br /&gt;It has a static method to get an instance of the class. It does an ordered lookup to determine the implementation of the Factory class to load.&lt;br /&gt;&lt;br /&gt;-&gt;Use the javax.xml.parsers.DocumentBuilderFactory system property. &lt;br /&gt;&lt;br /&gt;-&gt;Use the properties file "lib/jaxp.properties" in the JRE directory. This configuration file is in standard java.util.Properties format and contains the fully qualified name of the implementation class with the key being the system property defined above. The jaxp.properties file is read only once by the JAXP implementation and it's values are then cached for future use. If the file does not exist when the first attempt is made to read from it, no further attempts are made to check for its existence. It is not possible to change the value of any property in jaxp.properties after it has been read for the first time. &lt;br /&gt;&lt;br /&gt;-&gt;Use the Services API (as detailed in the JAR specification), if available, to determine the classname. The Services API will look for a classname in the file META-INF/services/javax.xml.parsers.DocumentBuilderFactory in jars available to the runtime. &lt;br /&gt;&lt;br /&gt;-&gt;Platform default DocumentBuilderFactory instance. &lt;br /&gt;&lt;br /&gt;Now we call the newDocumentBuilder method to obtain a class that will have APIs to convert XML documents to DOM document instances. The DocumentBuilder has a method called newDocument() that returns an object of type Document.&lt;br /&gt;&lt;br /&gt;If you wish to see which classes provide the implementation for the above interfaces you can do it using the following snippet.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;System.out.println(" Factory implementation class : " + DocumentBuilderFactory.newInstance().getClass()); &lt;br /&gt;System.out.println(" Builder implementation class : " + DocumentBuilderFactory.newInstance().newDocumentBuilder().getClass());&lt;br /&gt;System.out.println(" Document implementation class : " + DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument().getClass()); &lt;br /&gt;The output could vary from system to system but most probably would be this:&lt;br /&gt;&lt;br /&gt;Factory implementation class : class com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl&lt;br /&gt;&lt;br /&gt;Builder implementation class : class com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl&lt;br /&gt;&lt;br /&gt;Document implementation class : class com.sun.org.apache.xerces.internal.dom.DocumentImpl&lt;br /&gt;&lt;br /&gt;Thus we see that the factory pattern here helps us manufacture the correct implementation of another factory, which in turn leads us to correct objects.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/186002073186625662-2174926912010055714?l=shreyas735.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://shreyas735.blogspot.com/feeds/2174926912010055714/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=186002073186625662&amp;postID=2174926912010055714' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/2174926912010055714'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/186002073186625662/posts/default/2174926912010055714'/><link rel='alternate' type='text/html' href='http://shreyas735.blogspot.com/2008/07/abstract-factory-patterns.html' title='Abstract Factory Patterns'/><author><name>Parth</name><uri>http://www.blogger.com/profile/05955499680718949722</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
