java.io.file反斜杠保存在weblogic的root中

时间:2014-07-18 19:32:29

标签: java file weblogic java-io fileoutputstream

我有一个运行到weblogic服务器的java应用程序。

应用程序必须将文件写入路径\bla\john doe (例如)

为此,我使用了java.io.File库:

1. Verify if the path exists
2. If not, create it.
3. Verify if the file exists
4. if not, create it
5. Write the bytes into the file.

正确的行为是将目录bla创建到weblogic当前域的根目录中,然后在其中创建john doe

问题是:在我目前的环境中,它就像一个魅力,但在客户端,应用程序不会将反斜杠视为一个路径的元素,而不是创建两个目录,应用程序只创建一个,字面上命名为\bla\john does

所以,而不是:

-domain_root
    -bla
        -john does

我得到以下内容:

-domain_root
    -\bla\john does

(如果我逃避它,会出现相同但有两个反斜杠)

奇怪的是,如果我使用commom斜杠(/bla/john doe),它就可以工作..

-domain_root
    -bla
        -john does

有人知道可能会发生什么吗?


用于检查路径的

脚本

public File checkPath(String path) {
    File f = new File(cls_Util.NeutralizeFilePath(path));
    if (!(f.exists() && f.isDirectory())) {
        try {
            f.mkdirs();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    return f;
}
用于检查文件的

脚本:

public File checkFile(String path){
    File f = new File(path);
    return checkFile(f);
}

public File checkFile(File f) {
    if (!(f.exists() && f.isFile())) {
        try {
            f.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    return f;
}

创建文件的脚本

public File writeFile(String path, byte[] binaryfile) {
    File file = checkFile(path);

    if (file != null) {
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(path);
            try {
                fos.write(binaryfile);
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return file;
    }
    return null;
}

并创建文件:

String filePathPub = pathPub + newName;
File FilePathPub = writeFile(filePathPub, p_Arquivo);

1 个答案:

答案 0 :(得分:1)

在Windows上,\启动绝对路径;在Unix / Linux上,反斜杠是一个有效的文件名字符(因此启动一个相对路径)。

如果您不熟悉语义,我建议您尽量避免使用文件名连接平台特定的分隔符:

File current = new File();
File bla = new File(current, "bla");

(或者简单地坚持使用/(Unix使用的正斜杠)来分隔路径组件)。 Java会自动将其转换为Windows角色。