📜  Java的文件 copy() 方法及示例

📅  最后修改于: 2022-05-13 01:54:59.058000             🧑  作者: Mango

Java的文件 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)方法

方法一:使用copy(InputStream in, Path target, CopyOption... options)方法

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

参数:



  • 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();
        }
    }
}


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();
        }
    }
}


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
Number of bytes copied: 13

方法二:使用copy(Path source, OutputStream out)方法

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



参数:需要两个即

  • source:要复制数据的文件的路径
  • 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

方法三:使用copy(Path source, Path target, CopyOption... options)方法

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

参数:



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

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

例外:

  • 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