java - Windows/Linux 框上的文件分隔符问题

标签 java file junit io nio

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

一旦我将此代码放在 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);

    }

File Util 类

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“源或目标路径不存在”(引用上面的系统输出代码)

示例异常

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)

最佳答案

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

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

关于java - Windows/Linux 框上的文件分隔符问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36596340/

相关文章:

c - 文件读取问题 - 平行版生命游戏

java - Junit 测试在使用数据库和实体管理器时随机失败

java - 使用 jUnit 测试数据目录

java.lang.ClassNotFoundException : org. eclipse.aether.resolution.ArtifactResolutionException

java - 使用 addAsync 对 App Engine 中的延迟任务进行单元测试

java - 将父类成员仅限于其直接子类

html - 如何使用 Go 发布文件数组?

java - Scala 中的加泰罗尼亚数递归

c++ - 在 Mac 上通过 HTTP/HTTPS 和/或 FTP/FTPS 上传文件

java - JUnit 测试文件夹压缩