C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Assign Full Path and File name Program - Java 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.
INPUT: C:\Users\admin\Pictures\flower.jpg
OUTPUT:
Path: C:\Users\admin\Pictures\
File name: flower
Extension: jpg

class Path{ public static void main(String[] args){ String fp = "C:\\Users\\admin\\Pictures\\flower.jpg"; int last = fp.lastIndexOf('\\'); String p = fp.substring(0, last + 1); String fn = fp.substring(last + 1, fp.lastIndexOf('.')); String e = fp.substring(fp.lastIndexOf('.') + 1); System.out.println("Path: " + p); System.out.println("File name: " + fn); System.out.println("Extension: " + e); } }

Output

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