[英]Java - Composing String pathnames doesn't work
在以String
格式操作文件和目录的路径名时,我使用所有目录路径名以File.separator
结尾的约定。 这为我提供了一种简单的系统内方式来区分文件和目录,并且还允许我非常轻松地编写路径名。
Java 但是不遵循这个约定。 这意味着每次我从某个地方获取目录的路径名时,我都需要检查它是否以File.separator
,如果不是,则将字符串添加到路径名中。 看起来很容易,对吧?
我无法让它工作。
方法pathOfCurrentDirectory()
查找启动 JVM 的程序的目录,并返回其绝对路径名,并以File.separator
结尾。 createLogFile()
方法使用前面的方法创建日志文件的绝对路径名,如果需要,创建丢失的目录,创建文件,然后返回其绝对路径名。
pathOfCurrentDirectory()
中的检查使它看起来像它的 output 真正按预期以File.separator
结尾。 createLogFile()
中的检查使它看起来不像。 怎么了?
public static String createLogFile() {
String pathOfCurrentDirectory = pathOfCurrentDirectory();
String logDirectory = pathOfCurrentDirectory + "Log" + File.separator;
System.out.println();
System.out.println("pathOfCurrentDirectory: " + pathOfCurrentDirectory
+ "\n logDirectory = " + logDirectory);
String path = logDirectory + "Log_" + LogUtilities.dateId() + ".log";
System.out.println("logFile: " + path + "\n logFile.exists() = " + new File(path).exists());
FilesUtility.createNewFile(new File(path));
System.out.println("After creation, logFile.exists() = " + new File(path).exists());
return path;
}
public static String pathOfCurrentDirectory() {
String output = new File(System.getProperty("user.dir")).getAbsolutePath();
// System.out.println("Dir: " + output);
// System.out.println("File.separatorChar = " + File.separatorChar);
System.out.println("Last char of dir: " + output.charAt(output.length()-1));
System.out.println("modifiedDir = " + (output + File.separator));
String newOutput = (output + File.separator);
System.out.println("newOutput = " + newOutput);
// return output;
if( output.charAt(output.length()-1) == File.separatorChar )
return newOutput;
else return output;
}
Java 有 2 个专门用于操作路径名的类 - 文件和路径,如果您使用这些类,您几乎不需要使用File.separator
和风险字符串处理错误。
这显示同时使用文件和路径:
public static void main(String[] args) throws IOException
{
Files.createDirectories(Path.of(pathOfCurrentDirectory(), "Log"));
File f = createLogAsFile();
Path p = createLogAsPath();
}
private static Path createLogAsPath() throws IOException
{
Path path = Path.of(pathOfCurrentDirectory(), "Log", "Log_" + LogUtilities.dateId() + ".log");
System.out.println("createLogAsPath " + path + "\nFiles.isRegularFile(path) = " + Files.isRegularFile(path));
Files.createFile(path);
System.out.println("After creation, Files.isRegularFile(path) = " +Files.isRegularFile(path));
return path;
}
private static File createLogAsFile() throws IOException
{
File logDirectory = new File(pathOfCurrentDirectory(), "Log");
File path = new File(logDirectory, "Log_" + LogUtilities.dateId() + ".log");
System.out.println("createLogAsFile: " + path + "\n logFile.exists() = " + path.exists());
path.createNewFile();
System.out.println("After creation, logFile.exists() = " +path.exists());
return path;
}
public static String pathOfCurrentDirectory() {
return System.getProperty("user.dir");
}
您是否使用 File 或 Path 版本将取决于您的其他代码,尽管它们很容易在 toPath 或 toFile 调用之间切换。
你真的可以用你的createLogFile()
String filename = String.format("Log_%s.log", LogUtilities.dateId());
Path logfile = Paths.get("Log", filename);
FilesUtility.createNewFile(logfile.toFile());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.