Java example

Learn Java by Example: Java Program to Copy File

Java Program to Copy File In this tutorial, we will learn to copy files in Java.   The Java File class doesn’t provide any method to copy one file to another. However, we can use Java I/O Streams to read content from one file and write to another. Example: Copy files using i/o streams import java.io.FileInputStream; import java.io.FileOutputStream; class …

Learn Java by Example: Java Program to Get all Files Present in a Directory

Java Program to Get all Files Present in a Directory In this example, we will learn to list all the files and sub-directories present inside a directory.   Example 1: Java Program to List all files import java.io.File; class Main{ public static void main(String[] args){ // creates a file object File file = new File(“C:\Users\Guest …

Learn Java by Example: Java Program to Rename File

Java Program to Rename File In this tutorial, we will learn to rename file in Java.   The Java File class provides the renameTo() method to change the name of the file. It returns true if the renaming operation succeeds otherwise returns false. Example: Rename a File in Java import java.io.File; class Main{ public static void main(String[] args){ // create …

Learn Java by Example: Java Program to Create Directories

Java Program to Create Directories In this example, we will learn to create directories in Java.   The Java File class provides the mkdir() method to create a new directory. The method returns true if the new directory is created false if the directory already exists   Example 1: Create a new directory in Java import java.io.File; class Main{ …

Learn Java by Example: Java Program to Sort ArrayList of Custom Objects By Property

Java Program to Sort ArrayList of Custom Objects By Property In this program, you’ll learn to sort an arraylist of custom object by their given property in Java.   Example: Sort ArrayList of Custom Objects By Property import java.util.*; public class CustomObject{ private String customProperty; public CustomObject(String property){ this.customProperty = property; } public String getCustomProperty(){ …