SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

File Path, File Name and Extension Program in Java and Python

17 May 2014

ICSE 2014 solution to extract the file path, file name and extension separately from a full file path string.

Question:

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.

Example

INPUT: C:Users\admin\Pictures\flowers.jpg OUTPUT: Path: C:Users\admin\Pictures File name: flowers Extension: jpg

Algorithm:

Step 1: Start.

Step 2: Create a Scanner object to accept input from the user.

Step 3: Accept the full path of the file in a string variable s.

Step 4: Find the position of the last backslash using lastIndexOf('\\') and store it in x.

Step 5: Find the position of the last full stop using lastIndexOf('.') and store it in y.

Step 6: Extract the file path from index 0 to index x - 1.

Step 7: Extract the file name from index x + 1 to index y - 1.

Step 8: Extract the extension from index y + 1 to the end of the string.

Step 9: Display the path, file name and extension separately.

Step 10: Stop.

Explanation:

The full file path contains three useful parts: the folder path, the file name and the file extension. In a path such as C:Users\admin\Pictures\flowers.jpg, the last backslash separates the folder path from the actual file name. The last full stop separates the file name from the extension. Therefore the program does not need to check every folder separately. It only needs to locate these two important positions.

The method lastIndexOf('\\') is used to find the final backslash in the string. In Java, a backslash is written as '\\' because the backslash is an escape character. A single backslash is used inside strings and character literals to give special meanings to characters, such as newline. To represent the actual backslash character, it must be escaped with another backslash. The index returned by this method is stored in x.

Similarly, lastIndexOf('.') gives the position of the final dot in the file path. This is stored in y. The final dot is used because file names may sometimes contain other dots before the extension. Once the positions are known, substring() is used to extract the required parts. The path is the part before the last backslash. The file name starts just after the last backslash and ends just before the final dot. The extension starts just after the final dot and continues up to the end of the string.

For example, in C:Users\Java\awesome.txt, the last backslash is before awesome and the final dot is before txt. So the path becomes C:Users\Java, the file name becomes awesome and the extension becomes txt. This approach is short and clear because it uses built-in string library functions rather than manually counting characters.

Java Program:

Java
/**
* The class File_Q6_ICSE2014 inputs the full path of a file and
* outputs the file path, file name and file extension separately.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ICSE 2014 Question 6
*/

import java.util.Scanner;

class File_Q6_ICSE2014
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the full path of the file : ");
        String s = sc.nextLine();

        // Find the last backslash and the last dot in the file path.
        int x = s.lastIndexOf('\\');
        int y = s.lastIndexOf('.');

        String path = s.substring(0, x);
        String file = s.substring(x + 1, y);
        String extn = s.substring(y + 1);

        System.out.println("Output :");
        System.out.println("Path : " + path);
        System.out.println("File Name : " + file);
        System.out.println("Extension : " + extn);
    }
}

Equivalent Python Program:

Python
# Program to extract file path, file name and extension
# from a complete file path.

full_path = input("Enter the full path of the file : ")

# Find the last backslash and the last dot.
x = full_path.rfind("\\")
y = full_path.rfind(".")

path = full_path[:x]
file_name = full_path[x + 1:y]
extension = full_path[y + 1:]

print("Output :")
print("Path :", path)
print("File Name :", file_name)
print("Extension :", extension)

Output:

Enter the full path of the file : C:Users\Java\awesome.txt Output : Path : C:Users\Java File Name : awesome Extension : txt

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →