经过 10 年的黑客攻击,我的两便士是:
Windows 机器上的示例:
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: .\
底部附近的 linux 内容是 bash 如何在 Windows 系统上安装驱动器。
当前目录或工作“目录”实际上是您的程序所在的位置,但让我们使用它来跟踪我们正在处理的当前文件的位置。在powershell中输入pwd,结果称为路径!
目录总是以文件分隔符结尾并且从不包含文件名。它们可以很容易地附加。
“目录名”可以引用任意位置的任意目录(dirName + sep = dir)。
路径包括根目录、文件名或两者。
也就是说,路径可以通过将根、文件名或两者添加到目录来形成。(您可以区分 paths 和 file路径,然后“相对路径”将排除文件名,但将目录从基本目录提供给工作目录,尽管该术语变得多余,因为它被正确称为相对目录)。
注意关键字的不同含义:
然后将它们与完整路径的部分结合起来:
示例:根路径 = 根名称 + 根目录
注意这对 Windows 和 Linux 是如何工作的(根路径与根目录相同,因为根名称为空)。
在 Java 中,输出由以下方式产生:
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);
}
}
回答OP的问题:
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