Java参数验证,文件存在,可以读取并且是常规文件
我使用此代码验证Java参数验证,文件存在,可以读取并且是常规文件,java,validation,Java,Validation,我使用此代码验证java.io.File参数,该参数不应为null,应可访问,应为文件而非目录,等等: private static final String EXCEPTION_FILE_CAN_NOT_BE_READ = "The file %s does not seem to readable."; private static final String EXCEPTION_PATH_DOES_NOT_EXIST = "The path %s does not seem
java.io.File
参数,该参数不应为null
,应可访问,应为文件而非目录,等等:
private static final String EXCEPTION_FILE_CAN_NOT_BE_READ =
"The file %s does not seem to readable.";
private static final String EXCEPTION_PATH_DOES_NOT_EXIST =
"The path %s does not seem to exist.";
private static final String EXCEPTION_PATH_IS_NOT_A_FILE =
"The path %s does not seem to correspond to a file.";
private static final String EXCEPTION_PATH_REFERENCE_IS_NULL =
"The supplied java.io.File path reference can not be null.";
public static Banana fromConfigurationFile(
File configurationFile) {
if (configurationFile == null) {
String nullPointerExceptionMessage =
String.format(EXCEPTION_PATH_REFERENCE_IS_NULL, configurationFile);
throw new NullPointerException();
}
if (!configurationFile.exists()) {
String illegalArgumentExceptionMessage =
String.format(EXCEPTION_PATH_DOES_NOT_EXIST,
configurationFile.getAbsolutePath());
throw new IllegalArgumentException(illegalArgumentExceptionMessage);
}
if (!configurationFile.isFile()) {
String illegalArgumentExceptionMessage =
String.format(EXCEPTION_PATH_IS_NOT_A_FILE,
configurationFile.getAbsolutePath());
throw new IllegalArgumentException(illegalArgumentExceptionMessage);
}
if (!configurationFile.canRead()) {
String illegalArgumentExceptionMessage =
String.format(EXCEPTION_FILE_CAN_NOT_BE_READ,
configurationFile.getAbsolutePath());
throw new IllegalArgumentException(illegalArgumentExceptionMessage);
}
// ... more tests, like "isEncoding(X)", "isBanana(ripe)", ...
}
看起来有很多我可能会从某个地方“掐”的东西的样板。特别是因为这些并不是我需要的所有检查,还有更多检查(例如,文件是文本文件,具有正确的编码,…)。在我看来,有比这更简单的方法做这件事似乎是合情合理的。也许是一个FileSpecs对象通过构建器构造并传递给verifyFileSpecs静态帮助器
问题:我是做错了还是有代码可以重用
有效期后常见问题解答:
显示了我事先做了一些研究:我看了Java 6 SDK,这是我获得不同方法的地方,看了JDK 7和Files.isReadable,看了Apache Commons IO
这表明这个问题是独一无二的:我特别询问是否有可以重用的代码,我不是问“如何检查路径是否对应于文件而不是目录?”,所有这些问题都已经有了答案
为什么这对其他人有用:团队不喜欢提交给代码审查、签入和版本控制、潜在维护(单元测试等)的样板代码,因此,在我看来,从信誉良好的来源借用代码会非常有帮助。是的,我想说上面的代码不是干的(不要重复自己) 考虑使用apachecommons
public static Banana fromConfigurationFile(File configurationFile) {
Validate.notNull(configurationFile, String.format(EXCEPTION_PATH_REFERENCE_IS_NULL, configurationFile));
Validate.isTrue(configurationFile.exists(), String.format(EXCEPTION_PATH_DOES_NOT_EXIST, configurationFile.getAbsolutePath()));
Validate.isTrue(configurationFile.isFile()), String.format(EXCEPTION_PATH_IS_NOT_A_FILE, configurationFile.getAbsolutePath()));
// and more validation...
}
我喜欢它,但我不知道它。但是文件的细节呢?我在Apache Commons的FileUtils或类似的软件包中找不到任何东西…我认为
FileUtils
拥有类似于FileUtils\fileExists(file,message)
等的东西也不会是dry
,Kowser,你认为用可重用代码检查文件是否存在是个坏主意吗,例如,它是可读的,并且是一个文件而不是一个目录?你会如何做到这一点?我会考虑使用<代码>验证> /代码>作为首选。原因是,FileUtils
是文件的实用程序
类。由于存在文件#存在
,因此不必使用另一种方法来引发错误:)我的意思是Kowser需要一种类似于verifyFile(存在性、可读性和文件性质)或类似的方法。