【java工具方法】复制单个文件

    技术2022-07-14  69

    工具方法

    /** * 拷贝文件,将单个文件拷贝到另一个目录 * * @author 静心事成 * @param source 源文件 * @param target 目标文件 * @param isOverwrite 是否覆盖目标文件 * @return target.toPath() 目标文件路径 * @throws IOException * */ public static Path copy(File source, File target, boolean isOverride) throws IOException { // 使用java 7的新特性方法 return Files.copy(source.toPath(), target.toPath(), isOverride ? StandardCopyOption.REPLACE_EXISTING : StandardCopyOption.COPY_ATTRIBUTES); } /** * 拷贝文件,将单个文件拷贝到另一个目录 * copy(File, File, boolean)的重载方法 * * @param source 源文件路径 * @param target 目标文件路径 * @param isOverwrite 是否覆盖目标文件 * @return target.toPath() 目标文件路径 * @throws IOException * */ public static Path copy(String source, String target, boolean isOverride) throws IOException { return copy(new File(source), new File(target), isOverride); }

    引用的类

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

    相关方法

    【java工具方法】复制目录或文件

    示例

    public static void main(String[] args) { String source = "D:\\imgPath\\ewm.jpg"; String target = "D:\\imgPath\\ewm2.jpg"; try { copy(source, target, true); } catch (IOException e) { e.printStackTrace(); } }
    Processed: 0.009, SQL: 9