Copy file trong java bằng nhiều cách

Để sao chép file trong java Có một số cách để sao chép file.3 cách sẽ được hiển thị ở đây:

1.Sử dụng InputStream và OutputStream

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 

public class BasicFileCopier {
 
	public static void main(String[] args) {
 
		// Source file.
		File source = new File("C:/dev/file.txt");
 
		// Destination file.
		File destination = new File("C:/dev/files/file.txt");
 
		try {
			copyFile(source, destination);
		} catch (IOException e) {
			System.out.println("Problem occurs while copying files");
			e.printStackTrace();
		}
	}
 
	
	public static void copyFile(File source, File destination) throws IOException {
 
		// Using try with resources (No need to close files).
		try (InputStream inputStream = new FileInputStream(source); OutputStream outputStream = new FileOutputStream(destination);) {
 
			// Max length per line = 1024.
			byte[] buffer = new byte[1024];
 
			int lineLength;
			while ((lineLength = inputStream.read(buffer)) > 0) {
				outputStream.write(buffer, 0, lineLength);
			}
		}
 
	}
}

2.Sử dụng FileChannel

Bạn có thể sử dụng các phương thức transferFrom hoặc transfertTo  của lớp FileChannel để sao chép. Dưới đây là cách làm:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
 

public class FileChannelCopier {
 
	public static void main(String[] args) {
		
		// Source file.
		File source = new File("C:/dev/file.txt");
 
		// Destination file.
		File destination = new File("C:/dev/files/file.txt");
		
		try {
			copyFile(source, destination);
		} catch (IOException e) {
			System.out.println("Problem occurs when copying files");
			e.printStackTrace();
		}
	}
 
	
	public static void copyFile(File source, File destination) throws IOException {
		
		try (FileInputStream inputStream = new FileInputStream(source); 
			FileOutputStream outputStream = new FileOutputStream(destination);){
			
			FileChannel sourceChannel = inputStream.getChannel();
			FileChannel destinationChannel = outputStream.getChannel();
			
			destinationChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
		}
		
	}
}

 

3.Sử dụng java.nio.file.Files

Kể từ Java 1.7, lớp tĩnh java.nio.file.Files được đưa ra để hoạt động trên các tập tin và thư mục.Dưới đây là cách sử dụng:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
 
 

public class FilesCopier {
 
	public static void main(String[] args) {
 
		// Source file.
		Path source = Paths.get("C:/dev/file.txt");
 
		// Destination file.
		Path destination = Paths.get("C:/dev/files/file.txt");
		
		try {
			copyFile(source, destination);
		} catch (IOException e) {
			System.out.println("Problem occurs when copying files");
			e.printStackTrace();
		}
	}
 
	
	public static void copyFile(Path source, Path destination) throws IOException {
		Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
	}
}

 

x