Java programming

Learn Java by Example: Java Program to Create custom exception

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 …

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

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{ …

Learn Java by Example: Java Program to Get the File Extension

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(); …

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

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 …