【问题标题】:Problems with duplicate filenames when creating via widget通过小部件创建时重复文件名的问题
【发布时间】:2014-08-20 20:58:10
【问题描述】:

我有一个小部件,允许用户将电子邮件消息或文件拖放到小部件中,以将其复制到他们的文件系统中。这是 OpenNTF 中的 FileExplorer 项目,由比我更有经验的人设计。如果当前文件名已经存在于他们放置它的位置,我想修改它以提供新的文件名。对于电子邮件,我希望能够获取发件人和日期,但是当我在拖放电子邮件期间尝试访问文件内容时,我一直在抛出错误。

所以,我的问题其实很简单。我有'if'来确定文件名是否被采用,但我不知如何测试文件名的多个选项(比如编号然后'file1.eml','file2.eml','文件 3.eml')。我尝试在下面插入 DUPLICATE 这个词,但我不高兴。

try {
    if (source.isDirectory()) {
        File dirTarget = new File(fDest.getAbsoluteFile() + File.separator + source.getName());
        if (!dirTarget.exists()) {
            dirTarget.mkdir();
        }
        copyDir(monitor, source, dirTarget);
    }
    if (source.isFile()) {
        File dest = new File(fDest.getAbsolutePath() + File.separator + source.getName());

        if (dest.getAbsolutePath().compareTo(source.getAbsolutePath()) != 0) {
            copyFile(monitor, source, dest);
        } else {
            dest = new File(fDest.getAbsolutePath() + File.separator + "DUPLICATE" + File.separator + source.getName());
            copyFile(monitor, source, dest);
        }
    }
} catch (IOException e) {
}

供参考,copyFile方法的参数是

private void copyFile(IProgressMonitor monitor, File fSource, File fTarget) throws IOException

【问题讨论】:

  • 不是 XPages 问题。 Notes 客户端和 Java

标签: java eclipse rcp lotus-notes


【解决方案1】:

您需要构建不同的文件名。

  File.seperator

结果为 / \ 或 : 取决于您的平台,因为它是分隔 the directory from the file 的字符。

由于您正在删除文件,因此您不需要检查目录,这取决于您。您需要一个循环来测试文件名。为了方便使用 (DUPLICATE 1) (DUPLICATE 2) 等。像这样的东西:

private final static String DUPLICATE = "DUPLICATE";

private void copyOut(File source, File fDest, Monitor monitor) { 
   try {
         if (!source.exists() || !fDest.exists()) {
            // one or two files missing, can't copy
            // handle error here!
         } else {
           String destName = fDest.getAbsolutePath()+ File.separator + source.getName();
           File dest = new File(destName);

           if (source.isDirectory()) {
               if (!dest.exists()) {
                  destPath.mkdirs(); // Fix missing
               } else if (dest.isFile()) {
                 // Raise an error. Destination exists as file source is directory!!!
               }
           } else  { // We checked for existence and dir, so it is a file
             // Don't overwrite an existing file  
             dest = this.checkforDuplicate(dest); 
           }

           copyFile(monitor, source, dest);
        }
      } catch (IOException e) {
        // Error handling missing here!
      }
}

private File checkforDuplicate(File dest) {
    if (!dest.exists()) {
        return dest;
    }
    int duplicateNum = 1; 
    while (true) {
        ArrayList<String> pieces = Arrays.asList(dest.getAbsolutePath().split("."));
        pieces.add(pieces.size()-1, DUPLICATE);
        if (duplicateNum > 1) {
            pieces.add(pieces.size()-1,Integer.toString(duplicateNum));
        }
        duplicateNum++;
        StringBuilder newName = newStringBuilder();
        for (String s : pieces) {
            newName.append(s);
            newName.append(".");
        }
        // Strip the last .
        String outName = newName.substring(0, newName.length()-2);
        File result = new File(outName);
        if (!result.exists()) {
            return result;
        }

    }
}

检查代码,注销内存,会包含错别字。也不处理不包含点的文件名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 2017-04-24
    • 2015-12-27
    • 1970-01-01
    • 2019-06-28
    相关资源
    最近更新 更多