Analyzing file pathnames

exercise No. 174

Q:

Create your initial project:

  1. Download project.zip.

  2. Extract the archive

  3. Open the resulting Task/pom.xml file in your IntelliJ IDE as a project.

Depending on your operating system of choice absolute (=fully qualified) filenames are being represented differently:

Windows:

C:\Users\student\Desktop\Downloads\avira_v3.zip

Such path names may be decomposed into:

Drive C:
Path \Users\student\Desktop\Downloads
file base name avira_v3
file extension zip
UNIX-like (Mac-OS, Linux,...):

/usr/share/gpa/gpa-logo.png

Possible decomposition:

Drive - (not applicable)
Path /usr/share/gpa
file base name gpa-logo
file extension png

Implement a class FileMetaInfo to be instantiated from strings describing absolute or relative filenames. Your class shall have several attributes corresponding to the previously described values. Consider the following usage example:

Code
final FileMetaInfo fmi = new FileMetaInfo("C:\\users\\heinz\\Desktop\\index.html");

IO.println("Filename is relative? " + fmi.isRelative);
IO.println("Drive letter:" + fmi.drive);
IO.println("Directory path: " + fmi.path);
IO.println("File basename: " + fmi.basename);
IO.println("File extension: " + fmi.extension);
Result
Filename is relative? false
Drive letter:C
Directory path: \users\heinz\Desktop
File basename: index
File extension: html

Due to the possible presence of drive letters Windows pathnames are more difficult to parse. We illustrate a possible strategy dissecting C:\programms\openvpn\conf\hdm.ovpn into the desired constituents:

Decomposing path components:

C:\programms\openvpn\conf\hdm.ovpn → {"C:", "programms", "openvpn", "conf", "hdm.ovpn"}.

Drive letter string to character normalization

"C:" → 'C'

Decomposing filename (if required)

"hdm.test.ovpn" → {"hdm.test", "ovpn"}

Hints:

  1. You will have to deal with absolute and relative (e.g. ../../Desktop/var.png) filename specifications.

  2. You will have to deal with files having no extensions like e.g. /usr/bin/firefox. In this case the extension attribute is expected to be null.

  3. For the sake of limiting complexity do not consider filenames starting with a period (like /home/heinz/.bashrc).

  4. Read Java File Separator vs File Path Separator.

  5. Assembling path components may be effected by either using a StringBuffer instance. Mind the distinction between relative and absolute paths!

  6. The backslash \ must be escaped when being part of a Java String literal:

    String path = "C:\\programms\\openvpn\\conf\\hdm.ovpn";

A: