Java7 / 枚举构造函数 / Files.createTempDirectory(String prefix, FileAttribute>... attrs)
我想为枚举构造函数创建一个 Path 实例:
/** Temporary paths. */
public enum PATHS {
/** First temporary directory. */
PATH1(Files.createTempDirectory(new StringBuilder("tnk").append(File.separator).append("path1")
.toString())),
/** Second temporary directory. */
PATH2(Files.createTempDirectory(new StringBuilder("tnk").append(File.separator).append("path2")
.toString()));
/** {@link Path} reference. */
final Path mPath;
/**
* Constructor.
*
* @param pPath
* {@link Path} reference
*/
PATHS(final Path pPath) {
mPath = pPath;
}
/**
* Get {@link File} associated with the path.
*
* @return {@link File} reference
*/
public File getFile() {
return mPath.toFile();
}
}
Files.createTempDirectory(String, FilleAttribute atts)
抛出已检查的异常 (IOException) 但如何捕获或抛出异常或者更准确地说我如何处理异常?似乎是一个转储问题,但我现在不知道。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请在构造函数中处理它。
额外的好处是,它还可以最大限度地减少这种特殊情况下的代码重复。
话虽如此,我真的会仔细考虑汤姆·霍廷想要告诉你的事情。
Handle it in the constructor instead.
Additional benefit is that it also minimizes code duplication in this particular case.
Said that, I'd really think twice about what Tom Hawtin is trying to tell you.
实际上,任何涉及状态的事情都不应该用静态字段来完成。所以简单地让“PATHS”成为一个普通的类。创建一次,然后将其赋予应该使用它的对象。更好的设计应该改进错误处理、测试、依赖性、安全性等。
Really anything touching state should not be done with static fields. So simply makes "PATHS" a normal class. Create it once, and endow it to objects that should be using it. The better design should improve error handling, testing, dependency, security, etc.