Java example

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 …

Learn Java by Example: Java Program to implement private constructors

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 …