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

查看:22
本文介绍了在 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);

问题是 Eclipse 说文件类型中的方法 copy(Path, Path, CopyOption...) 不适用于参数 (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.

并且要获得与文件系统上的路径匹配的 Path 对象,您可以:

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,Path 有一个 .toFile() 方法.

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

请注意,如果您不幸使用返回 File 对象的 API,您可以随时执行以下操作:

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.

EDIT 可以使用 NIO 使用 1.6 将文件复制到另一个文件;请注意,Closer 类是由 番石榴:

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天全站免登陆