在Java中复制文件并替换现有目标

7vhp5slm  于 2023-05-27  发布在  Java
关注(0)|答案(5)|浏览(118)

我正在尝试复制一个java.nio.file文件,文件如下:

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

问题是Eclipse说“类型Files中的方法copy(Path,Path,CopyOption...)不适用于参数(File,String,StandardCopyOption)”
我在Win7 x64上使用Eclipse和Java 7。我的项目设置为使用Java 1.6兼容性。
是否有解决方案,或者我是否必须创建这样的东西作为解决方案:

File temp = new File(target);

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

谢谢。

pbwdgjma

pbwdgjma1#

您需要传递Path参数,如错误消息所述:

Path from = cfgFilePath.toPath(); //convert from File to Path
Path to = Paths.get(strTarget); //convert from String to Path
Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING);

这假设您的strTarget是有效路径。

t0ybt7op

t0ybt7op2#

作为对@assylias回答的补充:
如果您使用Java 7,请完全删除File。你想要的是Path
要获得与文件系统上的路径匹配的Path对象,您可以这样做:

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

很快就习惯了。请注意,如果您仍然使用需要File的API,Path有一个.toFile()方法。
请注意,如果你遇到了一个返回File对象的API,你总是可以这样做:

theFileObject.toPath()

但是在你的代码中,使用Path。系统地毫不犹豫。

EDIT使用NIO将一个文件复制到另一个1.6版本的文件中,可以这样做;请注意,Closer类的灵感来自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();
    }
}
cbjzeqam

cbjzeqam3#

package main.java;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class CopyFileOnExist {

    public static void main(String[] args)  {

        Path sourceDirectory = Paths.get("C:/Users/abc/Downloads/FileNotFoundExceptionExample/append.txt");
        Path targetDirectory = Paths.get("C:/Users/abc/Downloads/FileNotFoundExceptionExample/append5.txt");

        //copy source to target using Files Class
        try {
            Files.copy(sourceDirectory, targetDirectory,StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    }

}
mtb9vblg

mtb9vblg4#

strTarget是一个“String”对象,而不是“Path”对象

ee7vknir

ee7vknir5#

你的语句Files.copy(path,Stringor address,StandardCopyOption)是这样的,当你给出一个String,你必须设置你想要保存文件的路径。So simple使用下面的语句:-
copy(path,Paths.get(String or address),StandardCopyOption)通过使用Paths.get将String转换为您必须保存文件的文件位置或路径源路径。

相关问题