Home / Programs / Write a program to assign a full path and file name as given below. Using library functions, extract and output the file path, file name and file extension separately as shown. InputC:\Users\admin\Pictures\flower.jpg OutputPath: C:\Users\admin\Pictures\File name: flowerExtension: jpg
Programming Example

Write a program to assign a full path and file name as given below. Using library functions, extract and output the file path, file name and file extension separately as shown.

Input
C:\Users\admin\Pictures\flower.jpg

Output
Path: C:\Users\admin\Pictures\
File name: flower
Extension: jpg

👁 100 Views
💻 Practical Program
📘 Step by Step Learning
Study this program carefully to understand the logic, output, and explanation in a structured way.

Information & Algorithm

Program Code

import java.util.Scanner;

public class RAnsariFilepathSplit
{
    public static void main(String args[]) {

        Scanner in = new Scanner(System.in);
        System.out.print("Enter full path: ");
        String filepath = in.next();

        char pathSep = '\\';
        char dotSep = '.';

        int pathSepIdx = filepath.lastIndexOf(pathSep);
        System.out.println("Path:\t\t" + filepath.substring(0, pathSepIdx));

        int dotIdx = filepath.lastIndexOf(dotSep);
        System.out.println("File Name:\t" + filepath.substring(pathSepIdx + 1, dotIdx));

        System.out.println("Extension:\t" + filepath.substring(dotIdx + 1));
    }
}

Output

Enter full path: C:\Users\HP\Pictures\data-types.png
Path:           C:\Users\HP\Pictures
File Name:      data-types
Extension:      png
Press any key to continue . . .

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.