當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。