Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过小部件创建时出现重复文件名问题_Java_Eclipse_Rcp_Lotus Notes - Fatal编程技术网

Java 通过小部件创建时出现重复文件名问题

Java 通过小部件创建时出现重复文件名问题,java,eclipse,rcp,lotus-notes,Java,Eclipse,Rcp,Lotus Notes,我有一个小部件,允许用户将电子邮件或文件拖放到小部件中,以将其复制到文件系统。这是OpenNTF中的FileExplorer项目,由比我更有经验的人设计。如果当前文件名已经存在于他们放置它的位置,我想修改它以提供一个新的文件名。对于电子邮件,我本希望能够抓取发件人和日期,但在拖放电子邮件的过程中,当我试图访问文件内容时,我一直在抛出错误 所以,我的问题其实很简单。我已经有了“if”来确定文件名是否被占用,但我无法确定如何测试文件名的多个选项(比如编号,然后是“file1.eml”、“file2.

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

所以,我的问题其实很简单。我已经有了“if”来确定文件名是否被占用,但我无法确定如何测试文件名的多个选项(比如编号,然后是“file1.eml”、“file2.eml”、“file3.eml”)。我试着在下面插入“复制”一词,但我没有任何乐趣

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

您需要构造不同的文件名

  File.seperator
结果为/\or:取决于您的平台,因为它是字符分隔的

由于您正在删除一个文件,因此不需要检查目录,这取决于您自己。您需要一个循环来测试文件名。为了便于使用(副本1)(副本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;
        }

    }
}
private final static String DUPLICATE=“DUPLICATE”;
私有void copyOut(文件源、文件fDest、监视器){
试一试{
如果(!source.exists()| |!fDest.exists()){
//缺少一个或两个文件,无法复制
//在这里处理错误!
}否则{
字符串destName=fDest.getAbsolutePath()+File.separator+source.getName();
File dest=新文件(destName);
if(source.isDirectory()){
如果(!dest.exists()){
destPath.mkdirs();//缺少修复
}else if(dest.isFile()){
//引发错误。目标存在,因为文件源是目录!!!
}
}else{//我们检查了存在性和dir,所以它是一个文件
//不要覆盖现有文件
dest=此。检查重复(dest);
}
复制文件(监视器、源、目标);
}
}捕获(IOE异常){
//此处缺少错误处理!
}
}
私有文件检查副本(文件目的地){
如果(!dest.exists()){
返回目的地;
}
int duplicateNum=1;
while(true){
ArrayList片段=Arrays.asList(dest.getAbsolutePath().split(“.”);
工件.添加(工件.尺寸()-1,重复);
如果(duplicateNum>1){
add(pieces.size()-1,Integer.toString(duplicateNum));
}
duplicateNum++;
StringBuilder newName=newStringBuilder();
用于(字符串s:个数){
newName.append(s);
新名称。追加(“.”);
}
//剥去最后一块。
String outName=newName.substring(0,newName.length()-2);
文件结果=新文件(outName);
如果(!result.exists()){
返回结果;
}
}
}

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

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