Month: April 2021

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 …

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

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

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

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

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 = “Programiz”; …

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

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:”); } // main …

Learn Java by Example: Java Program to pass lambda expression as a method argument

Java Program to pass lambda expression as a method argument In this example, we will learn to pass lambda expression as the method argument in Java. Example 1: Define lambda expressions as method parameters import java.util.ArrayList; class Main{ public static void main(String[] args){ // create an ArrayList ArrayList<String> languages = new ArrayList<>(); // add elements …