java文件复制功能实现

      Java是一门面标的目的对象编程说话,不仅接收了C++说话的各类长处,还摒弃了C++里难以理解的多担当、指针等概念,是以Java说话具有功能壮大和简单易用两个特征。Java说话作为静态面标的目的对象编程说话的代表,极好地实现了面标的目的对象理论,许可程序员以优雅的思维体例进行复杂的编程 。

      Java具有简单性、面标的目的对象、分布式、健壮性、平安性、平台自力与可移植性、多线程、动态性等特点 。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等  。

东西/原料

  • 电脑
  • myeclipse和intellij IDEA

方式/步调

  1. 1

    第一种:根基copy经由过程字节省。

    1、具体代码如下所示:

    import java.io.BufferedInputStream;

    import java.io.BufferedOutputStream;

    import java.io.ByteArrayOutputStream;

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileNotFoundException;

    import java.io.FileOutputStream;

    public class Test8 {

    public static void main(String[] args) {

    ByteArrayOutputStream bos = null;

    BufferedInputStream in = null;

    try {

    File file = new File("D:/Documents/Downloads/新建文件夹 (2)/代办署理合同.pdf");

    if (!file.exists()) {

    throw new FileNotFoundException("file not exists");

    }

    in = new BufferedInputStream(new FileInputStream(file));

    bos = new ByteArrayOutputStream((int) file.length());

    int buf_size = 1024;

    byte[] buffer = new byte[buf_size];

    int len = 0;

    while (-1 != (len = in.read(buffer, 0, buf_size))) {

    bos.write(buffer, 0, len);

    }

    copyFile(bos.toByteArray(), "d:/test.pdf");

    System.out.println(bos.toByteArray());

    } catch (Exception e) {}

    }

    public static void copyFile(byte[] fileByte, String filePath)

    throws Exception {

    File file = new File(filePath);

    FileOutputStream fs = new FileOutputStream(file);

    BufferedOutputStream bo = new BufferedOutputStream(fs);

    bo.write(fileByte);

    bo.close();

    }

    }

  2. 2

    第二种:借助于java.nio.channels.FileChannel实现复制文件。

    代码如下所示:

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.nio.channels.FileChannel;

    public class Test8 {

    public static void main(String[] args) throws Exception {

    File source = new File("d:/test.pdf");

    if (source.exists()) {

    File dest = new File("d:/test2.pdf");

    copyFileChannels(source, dest);

    } else {

    System.out.println("原文件不存在!");

    }

    }

    public static void copyFileChannels(File source, File dest)

    throws IOException {

    FileChannel inputChannel = null;

    FileChannel outputChannel = null;

    try {

    inputChannel = new FileInputStream(source).getChannel();

    outputChannel = new FileOutputStream(dest).getChannel();

    outputChannel.transferFrom(inputChannel, 0, inputChannel.size());

    } finally {

    inputChannel.close();

    outputChannel.close();

    }

    }

    }

  3. 3

    第三种:利用java7之后供给的java.nio.file.Files实现。

    代码如下:

    import java.io.*;

    import java.nio.file.Files;

    public class CopyTest {

    public static void main(String[] args) {

    File inFile = new File("E:/图片/捉妖记.jpg");

    File outFile = new File("E:/file/捉妖记.jpg");

    try {

    Files.copy(inFile.toPath(), outFile.toPath());

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    }

  4. 4

    第四种:借助于Apache办事器供给类org.apache.commons.io.FileUtils

    1、类在架包commons-io.jar中

    2、下载架包

    2.1 百度搜刮:commons-io.jar下载

    2.2 

    3、具体实现代码:

    import java.io.File;

    import org.apache.commons.io.FileUtils;

    public class Test8 {

    public static void main(String[] args) throws Exception {

    File source = new File("d:/test.pdf");

    if (source.exists()) {

    File dest = new File("d:/test3.pdf");

    FileUtils.copyFile(source, dest);

    } else {

    System.out.println("原文件不存在!");

    }

    }

    }

注重事项

  • jdk 1.6 ,jdk 1.8 myeclipse 2010 ,IDEA 2018.2.2
  • 上面复制文件的体例,若是文件名字已经存在则笼盖已有文件
  • 发表于 2019-05-10 20:01
  • 阅读 ( 1140 )
  • 分类:其他类型

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
admin
admin

0 篇文章

作家榜 »

  1. xiaonan123 189 文章
  2. 汤依妹儿 97 文章
  3. luogf229 46 文章
  4. jy02406749 45 文章
  5. 小凡 34 文章
  6. Daisy萌 32 文章
  7. 我的QQ3117863681 24 文章
  8. 华志健 23 文章

联系我们:uytrv@hotmail.com 问答工具