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(); …
Month: April 2021
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 { // …
Java Program to Create File and Write to the File In this example, we will learn to create files in Java and write some information to the file. Example 1: Java Program to Create a File // importing the File class import java.io.File; class Main{ public static void main(String[] args){ // create a file …
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 …
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”; …
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 …
Java Program to pass methods as arguments to other methods In this example, we will learn to pass methods as arguments to other methods in Java Example 1: Java program to pass method as a parameter to other method class Main{ // calculate the sum public int add(int a, int b){ // calculate sum …
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 …
Java Program to implement private constructors In this example, we will learn to implement private constructors in Java. Example 1: Java program to create a private constructor class Test{ // create private constructor private Test (){ System.out.println(“This is a private constructor.”); } // create a public static method public static void instanceMethod(){ // create …
Java Program to Call One Constructor from another In this example, we will learn how we can call one constructor from another constructor in Java. Example 1: Java program to call one constructor from another class Main{ int sum; // first constructor Main() { // calling the second constructor this(5, 2); } // second …