Saturday, February 27, 2016

How to Copy Non Empty Directory with Files in Java 7 - Example Tutorial

It's easy to copy a file or empty directory in Java as you can use Files.copy(fromPath, toPath) from Java 7, but, unfortunately, it's not as easy to copy a non-empty directory with all its files and subdirectories in Java, much like deleting a non-empty directory. There is no method in Java IO API which copies everything inside one directory to another. The copy(source, target, CopyOption...) method can copy directories, but files inside the directories are not copied. So the new directory will be empty even if the original directory contains files and folders. Similarly, the copy fails if target directory already exists, unless the REPLACE_EXISTING copy option is specified. No doubt that NIO 2 of Java 7 has made life easier for Java programmers and provides useful tools to deal with files and directories, the onus is now on Java developers to learn and make the best use of it. If you to learn more about NIO 2 features, see here.

Monday, February 22, 2016

3 Examples to Get the Id of an Element in jQuery

As a page author, the id of an HTML element is something you should know, but there are scenarios when the page is automatically generated or may be some form elements are generated by using frameworks like JSF. In that case, what do you do if you want to find id attribute of some elements in the page programmatically? How do you find those ids using jQuery? There are several ways to solve this problem, a couple of them we'll see in this article itself.  The jQuery API provides many methods to get any attribute or property including the id of an HTML element programmatically. In this article, I am going to share three easy ways to get the id of an element using jQuery.

Saturday, February 20, 2016

2 ways to Split String with Dot (.) in Java using Regular Expression

You can use the split() method of java.lang.String class to split a String based on the dot. Unlike comma, colon, or whitespace, a dot is not a common delimiter to join String, and that's why beginner often struggles to split a String by dot. One more reason for this struggle is the dot being a special character in the regular expression. If you want to split String on the dot you need to escape dot as \\. instead of just passing "." to  the split() method. Alternatively, you can also use the regular expression [.] to split the String by a dot in Java. The dot is mostly used to get the file extension as shown in our example. The logic and method are exactly same as earlier examples of split string on space and split the string on a comma, the only difference is the regular expression. Once you know how to write the regular expression, all these examples will be same for you. Java's regular expression is inspired by Perl.

Friday, February 19, 2016

How to add Columns to an Existing table in SQL Server

Adding a new column to an existing table with data is always tricky. You need to know what data is there, how much data is there, to gauge how long your query is gonna take to complete in production. Also, you cannot add NOT NULL columns into an existing table if they are not empty and you don't have a default value specified. If you know SQL then you probably know that you can add columns to an existing table in SQL Server using ALTER command. It not only allows you to add a column but to drop columns as well. You can also add or drop constraints using ALTER command. Btw, you need to be careful while doing anything with existing tables because of data inside, which presents some challenges while adding new columns or dropping existing ones.

Wednesday, February 17, 2016

How does Enhanced for loop works in Java?

It's a long time since JDK 1.5 introduced the enhanced for loop, almost 12 years ago in the year 2004, but still many Java developers don't know basics of enhanced for loop, also known as for each loop in Java. The enhanced for loop provides the cleanest way to loop through an array or collection in Java, as you don't need to keep track of counter or you don't need to call the hasNext() method to check whether there are more elements left. The key point, which many Java developer doesn't know about the enhanced for loop is that you can use any Object which implements the Iterable interface on for (: ) construct, you can even use enhanced for loop to loop through the array. Since JDK 8 also introduced a forEach() function to provide looping functionality, don't confuse when I say for each loop with that. In this article, we'll see how enhanced for loop works and couple of important points related to it.

Saturday, February 13, 2016

How to Loop Through An Array in Java with Example

There are multiple ways to loop over an array in Java e.g. you can use a for loop, an enhanced for loop, a while loop or a do-while loop. Since while and do-while needs a condition to terminate they often depend upon the content of the array e.g. stop when the current element is null or even or odd etc. If you just want to iterate over an array to access each element e.g. loop over an array and print each entry then you should use either for loop or the enhanced for loop. The traditional for loop uses a counter and allows you to iterate until the last element is reached i.e. counter is equal to the length of the array while enhanced for loop maintains that counter internally, allowing you to iterate without worrying about counts. This results in clean code and it also eliminates the possibility of one-off errors. In this article, I'll show you 2 ways to iterate over an array, first by using traditional for loop and second by using enhanced for loop of Java 1.5.

Sunday, February 7, 2016

java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer [Solution]

The error message clearly says that Java runtime is not able to find a class called ServletContainer for Jersey library. This is the Servlet class you have specified in the deployment descriptor of your application. It's similar to DispatcherServlet of Spring MVC and this error is similar to the error you get when DisplayServlet was not in the classpath.
Anyway, java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer comes when you are trying to use Jersey but have not added required dependency on your classpath e.g. those JAR files which contain the class "com.sun.jersey.spi.container.servlet.ServletContainer".

Thursday, February 4, 2016

How to get first and last character of String in Java - Example

You can get the first and last character of a String using charAt() method in Java. This method accepts an integer index and returns the corresponding character from String. Since Java String is backed by an array, their index is also zero-based, which means the first character resides at index zero and the last character is at index, length-1, where length is the number of characters in the String. You can get the length of String by calling the length() method. The charAt() method is not defined on java.lang.String class, but on its super interface java.lang.CharSequence, hence it will also work for StringBuffer and StringBuilder. This method returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing. If the char value specified by the index is a surrogate, the surrogate value is returned.