After 10 years of hacking around my two pence is:
- Be consistent
- Think recursion
Example on a Windows machine:
File separator: \
Line separator:
Base name: file
Extension: txt
Filename: file.txt
Drive name: C
Root name: C: (empty on linux)
Root dir: \
Root path: C:\
Base dir: Source\
Base path: C:\Source\
Sub dir: project\
Sub-sub dir: docs\
Relative dir: project\docs\
Relative path: project\docs\file.txt
Working dir: C:\Source\project\docs\
Full path: C:\Source\project\docs\file.txt (also 'Absolute path' or 'File path')
Linux drive dir: C\
Linux root path: \C\
Linux base path: \C\Source\
Parent dir: ..\
Current dir: .\
The linux stuff near the bottom is how bash mounts the drive on Windows systems.
The current directory, or working "directory", is really wherever your program is, but let's use it to keep track of where the current file is we are working on. Type pwd
into powershell and the result is called a path!
Directories always end with the file separator and never include the filename. They can easily be appended.
"Directory name" could refer to any directory in any position (dirName + sep = dir).
Paths include the root, the filename, or both.
That is, paths can be formed by adding either the root, filename, or both, to a directory. (you could differentiate between paths and file paths, the 'relative path' would then exclude the file name but give the directories from the base to the working directory, though the term becomes redundant as this is properly called the relative directory).
Notice the distinct meanings of keywords:
- name
- directory
- path
- separator
These are then combined with the parts of the full path:
Example: root path = root name + root directory
Notice how this works for both Windows and Linux (where the root path is the same as the root directory because the root name is empty).
In Java, the output is produced by:
package io;
import java.io.File;
import java.util.logging.Logger;
/**
* Directory, File, and Path conventions.
*
* Directories always end with the file separator and never include the filename. They can easily be appended.
* - "Directory name" could refer to any directory in any position (dirName + sep = dir).
*
* Paths include the root, the filename, or both.
*
* <em>On Windows, base directory names can be capitalised.</em>
*/
public class Main {
private static Logger logger = Logger.getLogger("io");
public static void main(String[] args) {
final String sep = File.separator;
final String lf = System.lineSeparator();
logger.info("File separator: " + sep);
logger.info("Line separator: " + lf);
String baseName = "file";
String ext = "txt";
String fileName = baseName + "." + ext;
String driveName = "C";
String rootName = driveName + ":";
String rootDir = sep;
String rootPath = rootName + rootDir;
String baseDir = "Source" + sep;
String basePath = rootPath + baseDir;
String subDir = "project" + sep;
String subSubDir = "docs" + sep;
String relDir = subDir + subSubDir;
String relPath = relDir + fileName;
String workDir = basePath + relDir;
String fullPath = basePath + relPath;
logger.info("Base name: " + baseName);
logger.info("Extension: " + ext);
logger.info("Filename: " + fileName);
logger.info(lf);
logger.info("Drive name: " + driveName);
logger.info("Root name: " + rootName + " (empty on linux)");
logger.info("Root dir: " + rootDir);
logger.info("Root path: " + rootPath);
logger.info(lf);
logger.info("Base dir: " + baseDir);
logger.info("Base path: " + basePath);
logger.info("Sub dir: " + subDir);
logger.info("Sub-sub dir: " + subSubDir);
logger.info("Relative dir: " + relDir);
logger.info(lf);
logger.info("Relative path: " + relPath);
logger.info("Working dir: " + workDir);
logger.info("Full path: " + fullPath + " (also 'Absolute path' or 'File path')");
logger.info(lf);
String linuxDriveDir = driveName + sep;
String linuxRootPath = rootDir + linuxDriveDir;
String linuxBasePath = linuxRootPath + baseDir;
logger.info("Linux drive dir: " + linuxDriveDir);
logger.info("Linux root path: " + linuxRootPath);
logger.info("Linux base path: " + linuxBasePath);
logger.info(lf);
String parentDir = ".." + sep;
String currDir = "." + sep;
logger.info("Parent dir: " + parentDir);
logger.info("Current dir: " + currDir);
}
}
To give an answer to the OP's question:
A) foo = base name
B) foo.src = file name
C) src = extension
D) .src = ? (file extension separator + extension)
E) C:\users\OddThinking\Documents\My Source\ = base path
F) Widget\foo.src = relative (file) path
G) Widget = directory name
H) C:\users\OddThinking\Documents\My Source\Widget\ = working path aka "working directory"
I) C:\users\OddThinking\Documents\My Source\Widget\foo.src = full path, absolute path, file path
stem
. – Clavicembalo