简体   繁体   English

如何在java中复制粘贴和剪切粘贴文件或文件夹?

[英]How to copy-paste, and cut-paste file or folder in java?

I made a desktop app in java with netbeans platform. 我使用netbeans平台在java中创建了一个桌面应用程序。 In my app I want to give separate copy-paste and cut-paste option of file or folder. 在我的应用程序中,我想给文件或文件夹单独的复制粘贴和剪切粘贴选项。

So how can I do that? 那我该怎么办呢? I tried Files.copy(new File("D:\\\\Pndat").toPath(),new File("D:\\\\212").toPath(), REPLACE_EXISTING); 我尝试了Files.copy(new File("D:\\\\Pndat").toPath(),new File("D:\\\\212").toPath(), REPLACE_EXISTING); . But I don't get the exact output. 但我没有得到确切的输出。

If there any other option then suggest me. 如果有其他选择,那么建议我。

In case of "cut-paste" you can use renameTo() like this: 如果是“cut-paste”,你可以像这样使用renameTo()

File source = new File("////////Source path");
File destination = new File("//////////destination path");

if (!destination.exists()) {
    source.renameTo(destination);
}

In case of "copy-paste" you need to read in Input and Output stream. 如果是“复制粘贴”,则需要读入输入和输出流。

Use FileUtils from apache io and do FileUtils.copyDirectory(sourceDir, destDir); 使用文件实用程序从Apache的IO和做FileUtils.copyDirectory(sourceDir, destDir);

You can also do the following file operations 您还可以执行以下文件操作

  • writing to a file 写入文件
  • reading from a file 从文件中读取
  • make a directory including parent directories 创建一个包含父目录的目录
  • copying files and directories 复制文件和目录
  • deleting files and directories 删除文件和目录
  • converting to and from a URL 转换为URL和从URL转换
  • listing files and directories by filter and extension 按过滤器和扩展名列出文件和目录
  • comparing file content 比较文件内容
  • file last changed date 文件上次更改日期

Download link for apache i/o jar. 下载 apache i / o jar的链接

I think this question relates to using the system clipboard for copying a file specified in a Java app and using the OS "Paste" function to copy the file to a folder. 我认为这个问题涉及使用系统剪贴板复制Java应用程序中指定的文件,并使用操作系统“粘贴”功能将文件复制到文件夹。 Here is a short instructional example that will show you how to add a single file to the OS clipboard for later doing an OS "Paste" function. 这是一个简短的教学示例,它将向您展示如何将单个文件添加到OS剪贴板,以便稍后执行OS“粘贴”功能。 Tweak as necessary and add error/exception checking as needed. 根据需要调整并根据需要添加错误/异常检查。

As a secondary, this code also places the file name on the clipboard so you can paste the file name into document editors. 作为辅助代码,此代码还将文件名放在剪贴板上,以便您可以将文件名粘贴到文档编辑器中。

package com.example.charles.clipboard;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

public class JavaToSystemClipboard {

public static void main(final String[] args) throws Exception {
    final File fileOut = new File("someFileThatExists");
    putFileToSystemClipboard(fileOut);

}

public static void putFileToSystemClipboard(final File fileOut) throws Exception {
    final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    final ClipboardOwner clipboardOwner = null;

    final Transferable transferable = new Transferable() {
        public boolean isDataFlavorSupported(final DataFlavor flavor) {
            return false;
        }
        public DataFlavor[] getTransferDataFlavors() {
            return new DataFlavor[] { DataFlavor.javaFileListFlavor, DataFlavor.stringFlavor };
        }
        public Object getTransferData(final DataFlavor flavor) {
            if (flavor.equals(DataFlavor.javaFileListFlavor)) {
                final List<String> list = new ArrayList<>();
                list.add(fileOut.getAbsolutePath());
                return list;
            }
            if (flavor.equals(DataFlavor.stringFlavor)) {
                return fileOut.getAbsolutePath();
            }
            return null;
        }
    };
    clipboard.setContents(transferable, clipboardOwner);
}
}

您可以使用FileOutputStreamFileInputStream自己编写内容,也可以使用Apache Camel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM