Monday , 16 October 2017
Home » ICSE Important Programs » [Question 6] ICSE 2014 Paper Solved (File Path Name)

[Question 6] ICSE 2014 Paper Solved (File Path Name)

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\flowers.jpg
Output:
Path: C:Users\admin\Pictures
File name: flower
Extension: jpg

Programming Code:

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

import java.io.*;
class File_Q6_ICSE2014
    {
    public static void main(String args[])throws IOException
        {
            BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
            System.out.print("Enter the full path of the file : ");
            String s = br.readLine();
            
            int x = s.lastIndexOf('\\'); // Finding position of last backward slash
            int y = s.lastIndexOf('.'); // Finding position of last '.'
            
            String path = s.substring(0,(x+1));
            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);
        }
    }

Note: For extracting the position of backslash, ” is not valid as is an escape character in java and is treated differently. So in order to override this we are have to use ” instead of ”

Output:

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

Check Also

[Question 3] ISC 2017 Computer Practical Paper Solved – Caesar Cipher

Solution of Program 3 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input a sentence and perform Caesar Cipher (Shift alphabets 13 places ahead)

6 comments

  1. what are we supposed to write here:-
    int x = s.lastIndexOf(‘\\’);
    int y = s.lastIndexOf(‘.’);
    in those brackets.. what we have to right there??

  2. I did not get why are there 2 backslashes to find the last index of only one backslash?

    • Because one backslash is treated as an incomplete escape character. So if you have to save or print a backslash ou will always have to use ‘\\’

  3. I didnt get it.
    There is no space in the input. So what is stored in x??

Leave a Reply

Your email address will not be published. Required fields are marked *