java 如何在java中复制粘贴和剪切粘贴文件或文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14082770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to copy-paste, and cut-paste file or folder in java?
提问by Jay
I made a desktop app in java with netbeans platform. In my app I want to give separate copy-paste and cut-paste option of file or folder.
我用 netbeans 平台用 java 制作了一个桌面应用程序。在我的应用程序中,我想为文件或文件夹提供单独的复制粘贴和剪切粘贴选项。
So how can I do that? I tried Files.copy(new File("D:\\Pndat").toPath(),new File("D:\\212").toPath(), REPLACE_EXISTING);
. But I don't get the exact output.
那我该怎么做呢?我试过了Files.copy(new File("D:\\Pndat").toPath(),new File("D:\\212").toPath(), REPLACE_EXISTING);
。但我没有得到确切的输出。
If there any other option then suggest me.
如果还有其他选择,请建议我。
回答by Alya'a Gamal
In case of "cut-paste" you can use renameTo()
like this:
在“剪切粘贴”的情况下,您可以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.
在“复制粘贴”的情况下,您需要读取输入和输出流。
回答by vels4j
Use FileUtilsfrom 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
- listing files and directories by filter and extension
- comparing file content
- file last changed date
- 写入文件
- 从文件中读取
- 创建一个包含父目录的目录
- 复制文件和目录
- 删除文件和目录
- 与 URL 相互转换
- 按过滤器和扩展名列出文件和目录
- 比较文件内容
- 文件上次更改日期
Download linkfor apache i/o jar.
apache i/o jar 的下载链接。
回答by Java42
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. 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. Tweak as necessary and add error/exception checking as needed.
我认为这个问题与使用系统剪贴板复制 Java 应用程序中指定的文件以及使用操作系统“粘贴”功能将文件复制到文件夹有关。这是一个简短的说明示例,将向您展示如何将单个文件添加到操作系统剪贴板,以便稍后执行操作系统“粘贴”功能。根据需要进行调整并根据需要添加错误/异常检查。
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);
}
}
回答by Abraham
You can write things by yourself using FileOutputStreamand FileInputStreamor you can used Apache Camel.
您可以使用FileOutputStream和FileInputStream自己编写东西,也可以使用Apache Camel。