java.io.InputStream represents a stream of bytes. Often when reading binary files or from a network, data is returned in an input stream format and we need to read that data and convert it to a string format so that it becomes meaningful. This post will list out various methods in which the content of an input stream can be converted to a java.lang.String. All the methods assume that you have a source input stream available. Method 1: Using Scanner class java.io.Scanner class has a constructor which takes a java.io.Inputstream as argument. Once an object of scanner is created, use its hasNext and next methods toRead More →

What is JSON? JSON stands for JavaScript Object Notation and has become the most popular format for transfer and storage of data since due to its light weight nature. Data between client and server can only be transferred in the form of a string. A json object can be easily converted to a string and vice-versa. Also, it is pretty easy to traverse across a json object for retrieving values. A json string or json object stores data in key-value format and it can also represent complex parent-child relationships thus making it a preferred way to store and transmit data. Why String to json objectRead More →

If you are connecting to MySQL version 8 from your java application, there are bright chances that you might have come across the following error java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed and if you are accessing a MySQL database from a web or application server such as Wildfly or Tomcat, then you would have encountered this error java.sql.SQLException: Cannot create PoolableConnectionFactory (Public Key Retrieval is not allowed) Solution Add allowPublicKeyRetrieval connection option of MySQL with a value of true to the JDBC connection string. Thus, your connection string should be modified as jdbc:mysql://<database server ip>:3306/databaseName?allowPublicKeyRetrieval=true Reason Since you are connecting to a database, youRead More →

What is MAC address MAC(Media Access Control) address of a device is a unique identity assigned to it. It is used to identify a computer over a network. Similar to IP address, MAC address of a device is also unique. A MAC address is assigned during the manufacture process of hardware and remains constant throughout its life. It is also referred to as device address or physical address. MAC address is only assigned to Network Interface Controllers. These are hardware devices that connect to a network. Since these devices connect a computer to a network, they act as an interface between the two, hence calledRead More →

Meaning of @Override annotation @Override annotation is used over methods defined in a class. When this annotation is written over a method, then this method : Might be declared in a super class and defined in sub-class. Might be defined in a super(or parent) class and re-defined in sub-class(overridden). Might be declared in an interface and defined in the class implementing the interface. Example of this annotation is given below. public abstract class ParentClass { /* * Implemented method in parent class */ public void implementedMethod() { System.out.println(“Parent class method”); } /* * Abstract method in parent class */ public void declaredMethod(); } public classRead More →

Scenario Suppose your application starts a process (a server embedded in the application, let’s say). The application may be running from a command line interface or a GUI. Now, you want that whenever user terminates the application either by typing Ctrl + C(control and c keys pressed together) on command line or pressing close button on the application GUI, the process started by the application is also terminated. Shutdown Hook is the solution A shutdown hook is a piece of code which executes before an application terminates. Java provides a mechanism to apply shutdown hooks in an application. This shutdown hook is executed in belowRead More →

Monitoring directory : Meaning & Reason Monitoring a directory or a folder means keeping a watch on that directory for events such as a new file created, a file removed or modified. A typical requirement for such monitoring would be to implement an auto refresh functionality where you show the contents of a folder. With active monitoring on a folder, you can display the accurate list of files when the directory contents are modified in the background. How to Monitor a directory in Java Java 7 introduced java.nio package which provides support for monitoring a directory for changes. It allows us to create a serviceRead More →

Why use File filters Many times it happens that only files of certain type are required. Type may mean files of certain extension or files having some definite characters in their name. There is no sense of showing or listing files which are of no use in that scenario. Consider an example where you need to upload image files to an application. When you click on Browse button, you would only be interested in viewing the list of image files(having .bmp, .jpg, .jpeg or .png extension) in the directory you select. It would be a hassle for you to select the desired file if allRead More →

What is an enum ? An enum is a kind of data type in java which can contain a fixed set of values. These values are constants. The enum can have any one value out of these fixed set of values. Enum Syntax An enum is created using keyword enum followed by a user defined name and some fixed set of values enclosed between { and }. Where is enum used An enum is utilized when a particular type can have multiple fixed set of constant values. For Example, your application can have different types of user roles. Instead of using constants for these roleRead More →