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; } // …
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 Exception class …
Java Program to Print object of a class In this tutorial, we will learn to print the object of a class in Java. Example 1: Java program to print the object class Test{ } class Main{ public static void main(String[] args){ // create an object of the Test class Test obj = new Test(); // …
Java Program to Create an enum class In this example, we will learn to create an enum class in Java. Example 1: Java program to create an enum class enum Size{ // enum constants SMALL, MEDIUM, LARGE, EXTRALARGE; public String getSize(){ // this will refer to the object SMALL switch(this) { case SMALL: return “small”; …
Java Program to Determine the class of an object In this example, we will learn to determine the class of an object in Java using the getClass() method, instanceof operator, and the isInstance() method. Example 1: Check the class of an object using getClass() class Test1{ // first class } class Test2{ // second …
Java Program to Count number of lines present in the file In this example, we will learn to count the number of lines present in a file in Java. Example 1: Java program to count the number of lines in a file using Scanner class import java.io.File; import java.util.Scanner; class Main{ public static void main(String[] …
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; class Main{ …
Java Program to Get the name of the file from the absolute path In this example, we will learn to get the name of the file from the absolute path in Java. Example 1: Get file name from the absolute path using getName() import java.io.File; class Main{ public static void main(String[] args){ // link to …
Java Program to Get the File Extension In this example, we will learn to get the file extension in Java. Example 1: Java Program to get the file extension import java.io.File; class Main{ public static void main(String[] args){ File file = new File(“Test.java”); // convert the file name into string String fileName = file.toString(); …
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 a new …
Java Program to Delete File in Java In this example, we will learn to delete file in using the File and Files class in Java. Example 1: Java Program to Delete a file using delete() import java.io.File; class Main{ public static void main(String[] args){ // creates a file object File file = new File(“JavaFile.java”); …
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 { // …