21xrx.com
2024-09-17 03:28:52 Tuesday
登录
文章检索 我的文章 写文章
Java编写代码实现文件复制功能
2023-06-14 21:16:11 深夜i     --     --
Java 文件复制 文件输入输出流

在开发中,实现文件复制的功能是非常常见的,通过Java来实现文件复制也是一种简单易懂的方式。在Java中,我们可以使用文件输入输出流来进行文件的读取和写入操作。

以下是一个示例代码,可以将一个文件复制到另外一个文件中:


public class FileCopy {

  public static void main(String[] args) {

    FileInputStream input = null;

    FileOutputStream output = null;

    try {

      input = new FileInputStream("source.txt");

      output = new FileOutputStream("target.txt");

      byte[] buffer = new byte[1024];

      int length;

      while ((length = input.read(buffer)) > 0) {

        output.write(buffer, 0, length);

      }

      System.out.println("文件复制完成!");

    } catch (IOException e) {

      e.printStackTrace();

    } finally {

      try {

        input.close();

        output.close();

      } catch (IOException e) {

        e.printStackTrace();

      }

    }

  }

}

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复