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 the target directory already exists, unless the REPLACE_EXISTING copy option is specified.

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 maybe some form elements are generated by using frameworks like JSF. In that case, what do you do if you want to find an 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.

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.

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 loop provides the cleanest way to loop through an array or collection in Java, as you don't need to keep track of the 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.

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.

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 the 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 the 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.