当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Files copy()用法及代码示例


java.nio.file.Files类的copy()方法用于将字节从文件复制到I /O流或从I /O流复制到文件。 I /O流是表示不同类型的源的输入源或输出目标,例如磁盘文件。

方法:根据传递的参数类型,Files类提供3种类型的copy()方法。

  1. 使用copy(InputStream in, Path target, CopyOption… options)方法
  2. 使用copy(Path source, OutputStream out)方法
  3. 使用copy(Path source, Path target, CopyOption… options)方法

方法1:使用copy(InputStream输入,Path目标,CopyOption…选项)

此方法用于将所有字节从输入流复制到文件。

参数:



  • in:将复制其数据的输入流
  • 目标:将数据复制到的文件的路径
  • 选项:描述数据复制方式的选项

返回类型:复制的字节数

异常:

  • IOException:如果在读取或写入时发生错误
  • FileAlreadyExistsException:如果目标文件已经存在并且不能被替换
  • DirectoryNotEmptyException:如果由于目标文件为非空目录而无法替换目标文件
  • UnsupportedOperationException:如果不支持选项描述的应对方式
  • SecurityException:如果安全管理器拒绝对目标文件的写访问

实现方式:

示例

Java


// Impoerting classes from java.nio package as
// this package is responsible for network linking
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
  
// Main Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Input custom string
        String text = "geeksforgeeks";
  
        // Path of the file where data is to be copied
        Path path = (Path)Paths.get("/usr", "local", "bin",
                                    "fileIn.txt");
  
        System.out.println("Path of target file:"
                           + path.toString());
  
        // Byte stream whose data is to be copied
        InputStream in
            = new ByteArrayInputStream(text.getBytes());
  
        // Try block to check for exceptions
        try {
  
            // Printing number of copied bytes
            System.out.println(
                "Number of bytes copied:"
                + Files.copy(
                    in, path,
                    StandardCopyOption.REPLACE_EXISTING));
        }
  
        // Catch block to handle the exceptions
        catch (IOException e) {
  
            // Print the line number where exception occured
            e.printStackTrace();
        }
    }
}

输出:

Path of target file:/usr/local/bin/fileIn.txt
Number of bytes copied:13

方法2:使用copy(路径源,OutputStream输出)方法

此方法用于将所有字节从文件复制到输出流。



参数:需要两个

  • 源:将要复制其数据的文件的路径
  • out:将在其中写入复制数据的输出流

返回类型:复制的字节数

异常:

  • IOException:如果在读取或写入时发生错误
  • SecurityException:如果安全管理器拒绝对目标文件的读取访问

实现方式:

示例

Java


// Impoerting classes from java.nio package as
// this package is responsible for network linking
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
  
// Main Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Path of file whose data is to be copied
        Path path = (Path)Paths.get("/usr", "local", "bin",
                                    "fileOut.txt");
  
        // Getting the path of source file
        System.out.println("Path of source file:"
                           + path.toString());
  
        // Output stream where data is to written
        OutputStream out = new ByteArrayOutputStream();
  
        // Try block to check for exceptions
        try {
            // Printing number of copied bytes
            System.out.println("Number of bytes copied:"
                               + Files.copy(path, out));
        }
  
        // Catch block to handle the exception
        catch (IOException e) {
  
            // Print the line number where exception occured
            e.printStackTrace();
        }
    }
}

输出:

Path of source file:/usr/local/bin/fileOut.txt
Number of bytes copied:13

方法3:使用复制(路径源,路径目标,CopyOption…选项)

此方法用于将文件复制到目标文件。

参数:

  • 源:将要复制其数据的文件的路径
  • 目标:将数据复制到的文件的路径
  • 选项:描述数据复制方式的选项

返回类型:复制数据的文件的路径

异常:

  • IOException:如果在读取或写入时发生错误
  • FileAlreadyExistsException:如果目标文件已经存在并且不能被替换
  • DirectoryNotEmptyException:如果由于目标文件为非空目录而无法替换目标文件
  • UnsupportedOperationException:如果不支持选项描述的应对方式
  • SecurityException:如果安全管理器拒绝对目标文件的写访问

实现方式:

示例

Java


// Impoerting classes from java.nio package as
// this package is responsible for network linking
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
  
// Main Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Path of file where data is to copied
        Path pathIn = (Path)Paths.get("/usr", "local",
                                      "bin", "fileIn.txt");
        // Path of file whose data is to be copied
        Path pathOut = (Path)Paths.get(
            "/usr", "local", "bin", "fileOut.txt");
  
        System.out.println("Path of target file:"
                           + pathIn.toString());
  
        System.out.println("Path of source file:"
                           + pathOut.toString());
  
        // Try block to check for exceptions
        try {
  
            // Printing number of bytes copied
            System.out.println(
                "Number of bytes copied:"
                + Files.copy(
                    pathOut, pathIn,
                    StandardCopyOption.REPLACE_EXISTING));
        }
  
        // Catch block to handle the exceptions
        catch (IOException e) {
  
            // Print the line number where exception occured
            e.printStackTrace();
        }
    }
}

输出:

Path of target file:/usr/local/bin/fileIn.txt
Path of source file:/usr/local/bin/fileOut.txt
Number of bytes copied:13

相关用法


注:本文由纯净天空筛选整理自abhinavjain194大神的英文原创作品 Files copy() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。