Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
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
Answer:
import java.util.Scanner;
public class FileName {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter file name and path: ");
String input = scanner.nextLine();
int indexOfLastBackslash = input.lastIndexOf('\\');
int indexOfDot = input.lastIndexOf('.');
String outputPath = input.substring(0, indexOfLastBackslash + 1);
String fileName = input.substring(indexOfLastBackslash + 1, indexOfDot);
String extension = input.substring(indexOfDot + 1);
System.out.println("Path: " + outputPath);
System.out.println("File Name: " + fileName);
System.out.println("Extension: " + extension);
}
}
C. Sir| Created: 18-Apr-2019 | Updated: 18-Apr-2019|ICSE2014