-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractical37.java
More file actions
36 lines (28 loc) · 1.26 KB
/
practical37.java
File metadata and controls
36 lines (28 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//Write a program to copy the content of one file to another file
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class practical37 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the source file path: ");
String sourceFile = sc.nextLine();
System.out.print("Enter the destination file path: ");
String destFile = sc.nextLine();
// Using try-with-resources to automatically close the streams
try (FileInputStream fis = new FileInputStream(sourceFile);
FileOutputStream fos = new FileOutputStream(destFile)) {
byte[] buffer = new byte[1024];
int bytesRead;
// Read from source and write to destination in chunks of 1024 bytes
while ((bytesRead = fis.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
System.out.println("File copied successfully!");
} catch (IOException e) {
System.out.println("An error occurred during file copy: " + e.getMessage());
}
sc.close();
}
}