Write a class that will parse the path to a file. A file path describes the loca
ID: 3902837 • Letter: W
Question
Write a class that will parse the path to a file. A file path describes the location that a file is on a computer. For example, take the following path: C:UsersBobDesktopexample.txt This path is saying that on the C drive, there is a folder named ‘Users’. In that ‘Users’ folder there is a folder called ‘Bob, in there is a folder called ‘Desktop’, and in the ‘Desktop’ folder there is a file called ‘example.txt’. You need to write a class called FilePath that has the following methods.
public class FilePath
{
// Field
private String path;
// The path variable will be something like this:
// C:UsersBobDesktopexample.txt
public FilePath(String p)
// Returns the name of the drive that the file is on.
// ex. The drive of the path C:UsersBobDesktopexample.txt is C
// ex. The drive of the path MyDrive:UsersBobDesktopexample.txt is MyDrive
public String getDrive()
// Returns the name of the file, including the extension
// ex. The name of the file at C:UsersBobDesktopexample.txt is example.txt
// ex. The name of the file at MyDrive:WindowsSystem32calc.exe is calc.exe
public String getFileName()
// Returns the name of the file extension
// ex. The file extension for C:UsersBobDesktopexample.txt is txt
// ex. The file extension for MyDrive:WindowsSystem32calc.exe is exe
// ex. The file extension for E:documentsrchive.tar.gz is gz
public String getFileExtension()
// Returns the name of the file without the any extensions
// ex. The file base name for C:UsersBobDesktopexample.txt is example
// ex. The file base name for MyDrive:WindowsSystem32calc.exe is calc
// ex. The file base name for E:documentsrchive.tar.gz is archive
public String getBaseName()
// Returns the path to the folder that the file is in
// ex. Absolute path for C:UsersBobDesktopexample.txt is C:UsersBobDesktop
// ex. Absolute path for MyDrive:log.txt is MyDrive:
// ex. Absolute path for E:documentsrchive.tar.gz is E:documents
public String getAbsolutePath()
}
Create a tester class which creates a FilePath object, and print all the information about it out. Use the string C:\Users\Bob\Desktop\example.txt to test your code.
Please write in Java and at a simple level
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
FilePath.java
-----
public class FilePath
{
// Field
private String path;
// The path variable will be something like this:
// C:UsersBobDesktopexample.txt
public FilePath(String p)
{
path = p;
}
// Returns the name of the drive that the file is on.
// ex. The drive of the path C:UsersBobDesktopexample.txt is C
// ex. The drive of the path MyDrive:UsersBobDesktopexample.txt is MyDrive
public String getDrive()
{
int index = path.indexOf(':');
return path.substring(0, index);
}
// Returns the name of the file, including the extension
// ex. The name of the file at C:UsersBobDesktopexample.txt is example.txt
// ex. The name of the file at MyDrive:WindowsSystem32calc.exe is calc.exe
public String getFileName()
{
int index = path.lastIndexOf("\");
return path.substring(index + 1);
}
// Returns the name of the file extension
// ex. The file extension for C:UsersBobDesktopexample.txt is txt
// ex. The file extension for MyDrive:WindowsSystem32calc.exe is exe
// ex. The file extension for E:documentsrchive.tar.gz is gz
public String getFileExtension()
{
int index = path.lastIndexOf(".");
return path.substring(index + 1);
}
// Returns the name of the file without the any extensions
// ex. The file base name for C:UsersBobDesktopexample.txt is example
// ex. The file base name for MyDrive:WindowsSystem32calc.exe is calc
// ex. The file base name for E:documentsrchive.tar.gz is archive
public String getBaseName()
{
int index1 = path.lastIndexOf("\");
int index2 = path.lastIndexOf(".");
return path.substring(index1 + 1, index2);
}
// Returns the path to the folder that the file is in
// ex. Absolute path for C:UsersBobDesktopexample.txt is C:UsersBobDesktop
// ex. Absolute path for MyDrive:log.txt is MyDrive:
// ex. Absolute path for E:documentsrchive.tar.gz is E:documents
public String getAbsolutePath()
{
int index = path.lastIndexOf("\");
return path.substring(0, index);
}
}
TestFilePath.java
========
public class TestFilePath {
public static void main(String[] args) {
String path = "C:\Users\Bob\Desktop\example.txt";
System.out.println("Testing for path " + path);
FilePath p = new FilePath(path);
System.out.println("Drive=> " + p.getDrive());
System.out.println("Filename=> " + p.getFileName());
System.out.println("File extension=> " +p.getFileExtension());
System.out.println("Base name=> " + p.getBaseName());
System.out.println("Absolute path=> " + p.getAbsolutePath());
}
}
output
----
Testing for path C:UsersBobDesktopexample.txt
Drive=> C
Filename=> example.txt
File extension=> txt
Base name=> example
Absolute path=> C:UsersBobDesktop
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.