Java7 / 枚举构造函数 / Files.createTempDirectory(String prefix, FileAttribute... attrs)

发布于 2024-12-17 05:16:49 字数 986 浏览 2 评论 0 原文

我想为枚举构造函数创建一个 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) 但如何捕获或抛出异常或者更准确地说我如何处理异常?似乎是一个转储问题,但我现在不知道。

I'd like to create a Path instance for an enum constructor:

/** 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)throws a checked exception (IOException) but how do I catch or throw the exception or more precisely how do I handle the exception? Seems to be a dump question, but I have no idea right now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-12-24 05:16:49

请在构造函数中处理它。

PATH1("path1"),
PATH2("path2");

final Path mPath;

PATHS(final String path) {
    try {
        mPath = Files.createTempDirectory(new StringBuilder("tnk").append(File.separator).append(path).toString());
    } catch (IOException e) {
        throw new ExceptionInInitializerError(e);
    }
}

额外的好处是,它还可以最大限度地减少这种特殊情况下的代码重复。

话虽如此,我真的会仔细考虑汤姆·霍廷想要告诉你的事情。

Handle it in the constructor instead.

PATH1("path1"),
PATH2("path2");

final Path mPath;

PATHS(final String path) {
    try {
        mPath = Files.createTempDirectory(new StringBuilder("tnk").append(File.separator).append(path).toString());
    } catch (IOException e) {
        throw new ExceptionInInitializerError(e);
    }
}

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.

儭儭莪哋寶赑 2024-12-24 05:16:49

实际上,任何涉及状态的事情都不应该用静态字段来完成。所以简单地让“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文