将带有Java的文件移动到.eml目录

将带有Java的文件移动到.eml目录,java,file,Java,File,我使用以下代码将.eml文件从一个目录移动到另一个目录: if(Files.exists(Paths.get(newDirectoryPath))){ try { Files.move(Paths.get(filePath), Paths.get(newDirectoryPath), REPLACE_EXISTING); } catch (IOException e) { // TOD

我使用以下代码将
.eml
文件从一个目录移动到另一个目录:

if(Files.exists(Paths.get(newDirectoryPath))){
            try {
                Files.move(Paths.get(filePath), Paths.get(newDirectoryPath), REPLACE_EXISTING);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        }else{
            new File(newDirectoryPath).mkdir(); 
            moveFile(filePath, newDirectoryPath); 
        }
目录的创建是正常的,但是当我将一个文件移动到新目录中时,该目录将变成一个
.eml
文件。为什么会这样?我错过了什么

更新:

以下是调试时的值:

filePath = "/Users/absolute/path/to/my/file/myfile.eml";
newDirectoryPath = "/Users/absolute/path/to/my/new/directory/CompleteKsc"

文件中的目标移动(源、目标、选项)
是移动的实际目标。使用
REPLACE_EXISTING
您的呼叫将删除现有目标(您的目录),然后将源移动到该名称。
只有当目录为空*时,才会删除该目录,否则带有的调用将抛出DirectoryNotEmptyException

如果要将文件移动到目录中同名的文件,必须将文件名附加到目标

有一个使用
newdir.resolve(…)
的示例,可以精确地执行您想要的操作

将您的原始代码转换为遵循该示例,可以得到以下结果:

public void moveFile(String source, String targetDir)
{
    Path dirpath = Paths.get(targetDir);

    if (Files.exists(dirpath)) {

        Path target = dirpath.resolve(targetDir);

        try {
            Files.move(Paths.get(source), dirpath.resolve(target), REPLACE_EXISTING);
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    else {
        new File(targetDir).mkdir();
        moveFile(source, targetDir);
    }
}

*
“空”包括其中只有元文件的目录;例如,在Mac OS X上,仅包含.DS\u存储元数据文件的目录被视为空。

来自javadoc:“在某些实现中,目录中包含创建目录时创建的特殊文件或链接的条目。在这种实现中,只有特殊条目存在时,目录被视为空的。”

运行此操作时,变量包含哪些值以及预期结果是什么?@KErlandsson请参见编辑的问题。一切如期而至。预期的结果是将此文件以及多个其他文件移动到新目录中。您可能希望添加,只有当目录为空时,才会删除该目录。如果它不为空,将引发异常。