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

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:\Users\Bob\Desktop\example.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:\Users\Bob\Desktop\example.txt

public FilePath(String p)

// Returns the name of the drive that the file is on.

// ex. The drive of the path C:\Users\Bob\Desktop\example.txt is C

// ex. The drive of the path MyDrive:\Users\Bob\Desktop\example.txt is MyDrive

public String getDrive()

// Returns the name of the file, including the extension

// ex. The name of the file at C:\Users\Bob\Desktop\example.txt is example.txt

// ex. The name of the file at MyDrive:\Windows\System32\calc.exe is calc.exe

public String getFileName()

// Returns the name of the file extension

// ex. The file extension for C:\Users\Bob\Desktop\example.txt is txt

// ex. The file extension for MyDrive:\Windows\System32\calc.exe is exe

// ex. The file extension for E:\documents\archive.tar.gz is gz

public String getFileExtension()

// Returns the name of the file without the any extensions

// ex. The file base name for C:\Users\Bob\Desktop\example.txt is example

// ex. The file base name for MyDrive:\Windows\System32\calc.exe is calc

// ex. The file base name for E:\documents\archive.tar.gz is archive

public String getBaseName()

// Returns the path to the folder that the file is in

// ex. Absolute path for C:\Users\Bob\Desktop\example.txt is C:\Users\Bob\Desktop\

// ex. Absolute path for MyDrive:\log.txt is MyDrive:\

// ex. Absolute path for E:\documents\archive.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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!