Tuesday, 27 February 2018

How to read a text file into ArrayList in Java

Prior to Java 7, reading a text file into an ArrayList involves lot of boiler plate coding, as you need to read the file line by line and insert each line into an ArrayList, but from Java 7 onward, you can use the utility method Files.readAllLines() to read all lines of a text file into a List. This method return a List of String which contains all lines of files. Later you can convert this List to ArrayList, LinkedList, or whatever list you want to. Btw, this the fourth article in the series of reading a text file in Java. In the earlier parts, you have learned how to read a file using Scanner and BufferedReader. Then, reading the whole file as String and finally reading a text file into array. This program is not very different from those in terms of fundamentals. We are still going to use read() method for Java 6 solution and will read all text until this method return -1 which signals the end of file.

Monday, 26 February 2018

Dealing with Files and Directories in Java - 10 Thing Java Developer Should Know

The File API is one of the important parts of any programming language or API and even though Java's file API both new and old, are powerful, they are not intuitive enough compared to other languages e.g. Python. Apart from knowing the essential classes and abstractions e.g. File, InputStream, OutputStream, Reader, Writer, Channel etc, you also need to know and remember some nitty gritty detail to avoid subtle issues. There are many articles out there on the internet which can teach you how to read and write data from the file but there are very few which will tell you to do it in right way.

Friday, 23 February 2018

How to create a ZIP File in Java? ZipEntry and ZipOutputStream Compression Example

Since compressing and archiving old log file is an essential housekeeping job in any Java application environment, a Java programmer should know how to compress files in .zip format and then how to read them programmatically if required. The JDK provides full support to create and read ZIP files in Java. There is a separate package java.util.zip to hold all classes related zipping and unzipping files and streams. In this series of article, you will learn how to use those classes e.g. ZipFile, ZipEntry, ZipInputStream, and ZipOutputStream etc. This is the second article about how to work with compressed archives in Java e.g. .zip files.

Wednesday, 21 February 2018

How to copy elements of one array to another array in Java - Arrays.copyOf and Arrays.copyOfRange Example

There are multiple ways to copy elements from one array in Java e.g. you can manually copy elements by using a loop, create a clone of the array, use Arrays.copyOf() method or System.arrayCopy() to start copying elements from one array to another in Java. Even though both allow you to copy elements from source to destination array, the Arrays.copyOf() is much easier to use as it takes the just original array and the length of the new array. But, this means you cannot copy subarray using this method because you are not specifying to and from an index, but don't worry there is another method in the java.util.Arrays class to copy elements from one index to other in Java, the Arrays.copyOfRange() method. Both methods are overloaded to copy different types of array.

Monday, 19 February 2018

How to deal with ConcurrentModificationException in Java? Beware while removing elements from ArrayList in loop

One of the common problem while removing elements from an ArrayList in Java is the ConcurrentModificationException. If you use classical for loop with the index or enhanced for loop and try to remove an element from the ArrayList using remove() method, you will get the ConcurrentModificationException but if you use Iterator’s remove method or ListIterator’s
remove() method, then you won’t get this error and be able to remove the element. It’s an unwritten rule in Java that while looping through the list, you should not add() orremove() elements until the collection supports fail-safe Iterator e.g. CopyOnWriteArrayList, which operate on a copy of list rather than the original list.

Friday, 16 February 2018

10 Examples of Array in Java

Along with the String, the array is the most used data structure in Java. In fact, String is also backed by a character array in Java and other programming languages. It's very important for a Java programmer to have good knowledge of array and how to do common things with array e.g. initialization, searching, sorting, printing array in a meaningful way, comparing array, converting an array to String or ArrayList and doing some advanced slicing and dicing operation with an array in Java.

Wednesday, 14 February 2018

What is difference between an int and Integer in Java?

The first and foremost difference between an int and Integer or a char and Character is that former is a primitive data type while later is a class, also known as wrapper class because they wrap primitive data type inside it. When you first start learning Java, you start with primitive data types e.g. int, long, char, byte, boolean, float, and double but slowly you learn about Object and sometime later you know about Integer, Long, Character, Byte, Boolean, Float, and Double. At this point in time, you may wonder, what is the real difference between an int and Integer? isn't both same? We can pass Integer where int is expected and vice-versa then why on the earth we have both int and Integer?

Monday, 12 February 2018

Can you Overload or Override main method in Java?

One of the common doubt among Java beginners while learning overloading and overriding is, whether it's possible to overload main in Java ? Can you override the main method in Java? How will JVM find if you change the signature of the main method as part of method overloading? etc. These are a good question and shows curiosity and application of knowledge of students, so if you are the tutor you must answer these questions. The short answer to, can we overload the main method in Java is Yes, you can overloading, nothing stops from overloading, but JVM will always call the original main method, it will never call your overloaded main method.

Friday, 9 February 2018

5 Free Eclipse and JUnit Online Courses for Java Developers

Eclipse and JUnit are two of the essential tools for Java Developers. Eclipse is a Java IDE, Integrated Development environment which allows you to code, run and debug Java program from a single window, while JUnit is a unit testing library which allows you to test your Java code automatically. You can write JUnit tests and then you can run them automatically using Maven or Gradle plugin at build time. You can further automate your build using Jenkins which means your Unit test will run continuously and automatically, reporting any break at the earlier possible opportunity.

Wednesday, 7 February 2018

Easy Fine-Grained Sorting with JDK 8

Java 8‘s introduction of streams and useful static/default methods on the Comparator interface make it easy to compare two objects based on individual fields’ values without need to implement a compare(T,T) method on the class whose objects are being compared.

Sunday, 4 February 2018

Comparing files in Java

I am creating a series of video tutorials for PACKT about network programming in Java. There is a whole section about Java NIO. One sample program is to copy a file via raw socket connection from a client to a server. The client reads the file from the disk, and the server saves the bytes as they arrive, to disk. Because this is a demo, the server and the client are running on the same machine and the file is copied from one directory to the exact same directory but a different name. The proof of the pudding is eating it: the files have to be compared.

Friday, 2 February 2018

5 ways to Compare String Objects in Java - Example

Here are many ways to compare String in Java e.g. you can use equals() and equalsIgnoreCase() for equality check and compare() and compareTo() for ordering comparison. You can even use the equality operator == to perform reference based comparison e.g. to check both the String reference variable points to the same object. In general, equals() is used to check whether the value of given String is same i.e. they contain same characters in the same sequence or not e.g. "Groovy".equals("Groovy") will be true if you compare them using equals() method. You can also use equalsIgnoreCase() to check if they are equal irrespective of case e.g. "Apple" and "apple" will be same if you compare them using equalsIgnoreCase() method.