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
Output:
Path: C:Users\admin\Pictures
File name: flower
Extension: jpg
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
No comments:
Post a Comment