Tag Archives: Java examples

Learn Java by Example: Java Program to Create a immutable class

Hits: 9 Java Program to Create a immutable class In this example, we will learn to create an immutable class in Java.   Example: Java program to create immutable class final class Immutable{ private String name; private int date; Immutable(String name, int date) { // initialize the immutable variables this.name = name; this.date = date; …

Learn Java by Example: Java Program to Create custom exception

Hits: 1 Java Program to Create custom exception In this example, we will learn to create custom checked and unchecked exception in Java.   Example 1: Java program to create custom checked exception import java.util.ArrayList; import java.util.Arrays; // create a checked exception class class CustomException extends Exception{ public CustomException(String message){ // call the constructor of …

Learn Java by Example: Java Program to Get the relative path from two absolute paths

Hits: 5 Java Program to Get the relative path from two absolute paths In this example, we will learn to get the relative path from two absolute paths in Java using String methods, URI class, and java.nio.file package. Example 1: Get a relative path from two absolute paths using URI class import java.io.File; import java.net.URI; …

Learn Java by Example: Java Program to Delete Empty and Non-empty Directory

Hits: 3 Java Program to Delete Empty and Non-empty Directory In this example, we will learn to delete an empty directory, a non-empty directory, and a directory with non-empty subdirectory in Java.   Example 1: Java Program to delete an empty directory import java.io.File; class Main{ public static void main(String[] args){ try { // create …

Learn Java by Example: Java Program to Read the Content of a File Line by Line

Hits: 6 Java Program to Read the Content of a File Line by Line In this example, we will learn to read a the content of a file using various class in Java.   Example 1: Java Program to Read File Using BufferedInputStream import java.io.BufferedInputStream; import java.io.FileInputStream; class Main{ public static void main(String[] args){ try …

Learn Java by Example: Java Program to Load File as InputStream

Hits: 7 Java Program to Load File as InputStream In this example, we will learn to load a file as an input stream using the FileInputStream class in Java.   Example 1: Java Program to Load a Text File as InputStream import java.io.InputStream; import java.io.FileInputStream; public class Main{ public static void main(String args[]){ try { …

Learn Java by Example: Java Program to Convert a String into the InputStream

Hits: 1 Java Program to Convert a String into the InputStream In this program, we will learn to convert a string to an inputstream in Java.   Example: Java Program to convert String to InputStream import java.io.ByteArrayInputStream; import java.io.InputStream; import java.nio.charset.StandardCharsets; public class Main{ public static void main(String args[]){ // Creates a string String name …

Learn Java by Example: Java Program to Calculate the Execution Time of Methods

Hits: 5 Java Program to Calculate the Execution Time of Methods In this example, we will learn to calculate the execution time of normal methods and recursive methods in Java.   Example 1: Java Program to calculate the method execution time class Main{ // create a method public void display(){ System.out.println(“Calculating Method execution time:”); } …