Friday, October 30, 2020

BlueJ Program On String And File Path With File Name

 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 


       import java.util.*;
       class Ab
       {
            String str1,str2,str3;
            int i;
            Scanner sc=new Scanner(System.in);
            public void take()
            {
              System.out.print("\nEnter the file name with extension and complete path:");
              str1=sc.nextLine();
              i=str1.lastIndexOf('/');
              str2=str1.substring(0,i+1);
              System.out.print("\nPath of the file:"+str2);
              str2=str1.substring(i+1);// flower.jpg
              i=str2.indexOf('.');
              str3=str2.substring(0,i);
               str2=str2.substring(i+1);
              System.out.print("\n\nFile Name="+str3 + " Extension="+str2);
            }
        }
   Sample Input Output

Enter the file name with extension and complete path:C:/my folder/new folder/flower.jpg

Path of the file:C:/my folder/new folder/

File Name=flower Extension=jpg

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner