【问题标题】:File separator issues on windows/linux boxwindows/linux 盒子上的文件分隔符问题
【发布时间】:2016-04-13 11:00:17
【问题描述】:

下面的枚举帮助我避免在 JUNIT 测试用例的不同位置硬编码目录值。这似乎在 IDE 内的 WINDOW 盒子上的开发环境中工作。

只要我把这段代码放在一个 LINUX 机器上,JUNIT 类就找不到目录。 Files.exists(sourcePath) 返回 false。相同的代码在 IDE(Windows)上返回 true。关于这里出了什么问题的任何指示?

public enum DIRECTORY {

    OUTPUT("resources/output"), RESOURCE("resources/resource"), PROCESSED_OUTPUT(
            "resources/output/resources/resource"), EXPLODED_WEBAPPS(
            "temp/webapps"), WEBAPPS("webapps");

    private String _name;

    private DIRECTORY(String _name) {

        this._name = _name;

    }

    public String getDirectoryName() {
        return _name;
    }

}

枚举的示例使用代码

private void restore_csv_files() {

        Path _processed_output = Paths.get(DIRECTORY.PROCESSED_OUTPUT
                .getDirectoryName());
        Path resource = Paths.get(DIRECTORY.RESOURCE.getDirectoryName());

        FileUtility.copy_files(_processed_output, resource);
        FileUtility.delete_files(_processed_output);
        FileUtility.delete_directory(_processed_output);

    }

文件工具类

public static void copy_files(Path sourcePath, Path targetPath) {
        if (Files.exists(sourcePath) && Files.exists(targetPath)) {
            try {
                Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {

                    @Override
                    public FileVisitResult visitFile(final Path file,
                            final BasicFileAttributes attrs) throws IOException {
                        Files.copy(file,
                                targetPath.resolve(sourcePath.relativize(file)));
                        return FileVisitResult.CONTINUE;
                    }
                });
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            System.out.println("Either Source or Target path does not exist");
        }
    }

基本上在windows上这个东西可以工作,但是在linux机器上,得到一个sysout“源或目标路径不存在”(参考上面的sysout代码)

示例异常

java.nio.file.NoSuchFileException: webapps/web.war
    at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
    at sun.nio.fs.UnixFileAttributeViews$Basic.readAttributes(UnixFileAttributeViews.java:55)
    at com.temp.utils.FileUtility.alter_date_time_of_file(Unknown Source)
    at com.temp.webengine.WebEngineTest.test_web_app_updated_true(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)

【问题讨论】:

  • 资源不是文件,它们存在于 CLASSPATH 中,而不是文件系统中。重新思考。

标签: java file junit io nio


【解决方案1】:

对于文件分隔符,使用System Properties

static String separator = System.getProperty("file.separator");

【讨论】:

  • 我认为path.separator 对枚举DIRECTORY 也很有用
  • 你的意思是它失败的原因。所以你想让我在 ENUM 中使用路径分隔符吗?
  • 我认为这不是路径分隔符问题。如果您查看 ENUM,则文件分隔符已正确用于 linux (/)。如果它应该失败,它应该在 WINDOWS 机器而不是 linux 上失败。希望添加的异常应该引起一些关注
  • 你需要做一些调试,打印if语句前的绝对路径,检查文件是否真的存在。
猜你喜欢
  • 2020-10-22
  • 2013-12-11
  • 1970-01-01
  • 2012-04-14
  • 1970-01-01
  • 1970-01-01
  • 2012-02-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多