Skip to main content

Posts

Showing posts with the label Java

File Copy Using Java NIO

I wanted to post this small piece of code for those who are looking for it. Copying file contents using Streams is old way of doing, Java New IO is the way to go; using Java NIO is better in terms of performance. Following is the code to do file copy using Java NIO. public static void copyFile(File src, File dest) throws IOException {     if (!src.exists()) {         // either return quietly OR throw an exception         return;     }     if (!dest.exists()) {         dest.createNewFile();     }     FileChannel source = new FileInputStream(src).getChannel();     try {         FileChannel destination = new FileOutputStream(dest).getChannel();         try {           ...

Java Communications API – Configuration How To

Java Communication API is an extension from Sun to program for Serial/Parallel ports. Sun’s implementation of this specification can be found at " http://java.sun.com/products/javacomm ", there is also a third party Open Source implementation available at " http://rxtx.qbang.org/wiki/index.php/Main_Page ". Java is platform Independent, but you need some platform specific implementation to access Serial/Parallel ports, and that is why you need to do some extra work to use Java Communication API. First download the API binaries from " http://rxtx.qbang.org/wiki/index.php/Download " page. Extract it and follow the steps given below for Windows platform. If you are just trying execute programs already written using just JRE, then Copy rxtxParallel.dll to JRE_HOME\bin\ Copy rxtxSerial.dll to JRE_HOME\bin\ Copy RXTXcomm.jar to JRE_HOME\lib\ext\ If you are writing you own applications/programs, compiling and executing them, then Copy rxtxParalle...

Calculating Domain URL in Java Web Applications

When you are developing web applications, its obvious that you’ll write code to generate paths for resources like images, stylesheets, scripts, etc. You can do this by simply using relative paths. In your JSP application, you can do this using ‘getContextPath()’ method of ‘HttpServletRequest’ object. Lets see what this method gives you. It returns a String value, the returned string will always have a leading ‘/’ character and will never have a trailing ‘/’. This method returns an empty string when you deploy your application as ROOT context and returns the subdomain context when deployed as subdomain. For example, if you name your WAR file as ‘myExample.war’ and deploy it in TOMCAT [by placing the WAR file in ‘webapps’ directory]. The ‘getContextPath()’ method will return ‘/myExample’. In your JSP page, if you’ve a image tag pointing to an image resource and you want it to work even when deployed as subdomain you can do that by using this ‘getContextPath()’ method as used below. ...

Eclipse Tips

In few situations, you want eclipse to refresh the project contents automatically. Its might be required when you changed the one/more files of an eclipse project in some other editor and you wanted eclipse to reflect those changes automatically. If you are using version control and when you get latest changes from repository, you want those changes to be reflected on your eclipse. This can be configured in eclipse easily. Go to Window –> Preferences , in this  window select General –> Workspace from the tree in the left panel. When you select Workspace, you’ll see workspace related configuration options in the right panel. The top section will have three checkboxes, one of them will " Refresh Automatically ". Select this checkbox and click OK. Another feature which could be handy is to see different Icons for different file types like Interfaces, Classes, Abstract Classes, etc. You can set this in Window –> Preferences window. In this window select General –...

Simple DAO API Using JDBC

Update: Source code now available @ http://code.google.com/p/simple-dao-api/ This article is about the "Simple DAO API" which I wrote using JDBC API to assist database programming. This TINY LITTLE API containing 7 classes [Not exactly an API, you can not call a set of 7 classes as API I guess]; is INSPIRED by the SPRING DAO classes and is intended for those beginners who just started learning JDBC and trying their small examples, class assignments and for those who are trying to develop small desktop applications, small Servlet based web applications. For this kind of development it'll be difficult to use the those very nice features of APIs like Spring to ease database programming, as it requires bit of time to understand the Spring API, and most of the times you end up not using all the features of the API. You can find the links to download the source code at the end of this article. As I mentioned above these classes were written after getting inspired by...

String manipulation in Java

One topic most of the developers in early days of their career overlook while programming is STRING MANIPULATION . This article will throw some light on this topic, so that one can make habit of some efficient coding. I am going to give some insight into Java string manipulation and some better ways to do string manipulation. In Java String objects are immutable, what it means is once you create a String object in Java, it can not be changed/modified. For example look at the following code. String a = "A String Object"; a = "Second String Object"; In the above code, we created a String object 'a' at line#1 with the value "A String Object", and in line#2 we tried to change the value of the object 'a' to "Second String Object". As I mentioned little earlier, when you create a String object in Java, it can not be changed. In line#2 when you try to change the value of object 'a', JVM [Java Virtual Machine] will c...