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.
Input C:\Users\admin\Pictures\flower.jpg
Output Path: C:\Users\admin\Pictures\
File name: flower
Extension: jpg
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class program6
{
public static void main(String[] args)
{
int LastBackslashPos=0,DotPos=0;
String Path="",file="",extension="",fullPath="";
Scanner sc = new Scanner(System.in);
System.out.print("Enter file name and path: ");
fullPath = sc.nextLine();
LastBackslashPos = fullPath.lastIndexOf('\\');
DotPos = fullPath.lastIndexOf('.');
Path = fullPath.substring(0, LastBackslashPos + 1);
file = fullPath.substring(LastBackslashPos + 1, DotPos);
extension = fullPath.substring(DotPos + 1);
System.out.println("Path: " + Path);
System.out.println("File Name: " + file);
System.out.println("Extension: " + extension);
}
}