在Java中复制文件并替换现有目标 [英] Copy file in Java and replace existing target

查看:220
本文介绍了在Java中复制文件并替换现有目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图复制一个文件与java.nio.file.Files像这样:

I'm trying to copy a file with java.nio.file.Files like this:

Files.copy(cfgFilePath, strTarget, StandardCopyOption.REPLACE_EXISTING);



类型Files不适用于参数(File,String,StandardCopyOption)

The problem is that Eclipse says "The method copy(Path, Path, CopyOption...) in the type Files is not applicable for the arguments (File, String, StandardCopyOption)"

我在Win7 x64上使用Eclipse和Java 7。我的项目设置为使用Java 1.6兼容性。

I'm using Eclipse and Java 7 on Win7 x64. My project is set up to use Java 1.6 compatibility.

有一个解决方案,或者我必须创建这样的解决方法:

Is there a solution to this or do I have to create something like this as a workaround:

File temp = new File(target);

if(temp.exists())
  temp.delete();

谢谢。

推荐答案

作为@assylias的补充:

As a complement to @assylias' answer:

如果使用Java 7,请删除 File 完全。你想要的是 Path

If you use Java 7, drop File entirely. What you want is Path instead.

并得到路径对象匹配文件系统上的路径,您可以:

And to get a Path object matching a path on your filesystem, you do:

Paths.get("path/to/file"); // argument may also be absolute

使用它真的很快。请注意,如果您仍然使用需要 File 的API,路径 .toFile

Get used to it real fast. Note that if you still use APIs which require File, Path has a .toFile() method.

请注意,如果您使用的是返回 File 对象,您可以随时执行:

Note that if you are in the unfortunate case where you use an API which returns File objects, you can always do:

theFileObject.toPath()

但是在你的代码中,使用 Path 。系统。没有第二个想法。

But in code of yours, use Path. Systematically. Without a second thought.

编辑可以这样做使用1.6使用NIO将文件复制到另一个;请注意 Closer 类受到 Guava

EDIT Copying a file to another using 1.6 using NIO can be done as such; note that the Closer class is inspited by Guava:

public final class Closer
    implements Closeable
{
    private final List<Closeable> closeables = new ArrayList<Closeable>();

    // @Nullable is a JSR 305 annotation
    public <T extends Closeable> T add(@Nullable final T closeable)
    {
        closeables.add(closeable);
        return closeable;
    }

    public void closeQuietly()
    {
        try {
            close();
        } catch (IOException ignored) {
        }
    }

    @Override
    public void close()
        throws IOException
    {
        IOException toThrow = null;
        final List<Closeable> l = new ArrayList<Closeable>(closeables);
        Collections.reverse(l);

        for (final Closeable closeable: l) {
            if (closeable == null)
                continue;
            try {
                closeable.close();
            } catch (IOException e) {
                if (toThrow == null)
                    toThrow = e;
            }
        }

        if (toThrow != null)
            throw toThrow;
    }
}

// Copy one file to another using NIO
public static void doCopy(final File source, final File destination)
    throws IOException
{
    final Closer closer = new Closer();
    final RandomAccessFile src, dst;
    final FileChannel in, out;

    try {
        src = closer.add(new RandomAccessFile(source.getCanonicalFile(), "r");
        dst = closer.add(new RandomAccessFile(destination.getCanonicalFile(), "rw");
        in = closer.add(src.getChannel());
        out = closer.add(dst.getChannel());
        in.transferTo(0L, in.size(), out);
        out.force(false);
    } finally {
        closer.close();
    }
}

这篇关于在Java中复制文件并替换现有目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆