-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpCommand.java
More file actions
31 lines (27 loc) · 1008 Bytes
/
CpCommand.java
File metadata and controls
31 lines (27 loc) · 1008 Bytes
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
package IO;
import java.io.*;
public class CpCommand extends Command{
public CpCommand() {
super("cp");
}
@Override
public void action(String[] cmd) {
if (cmd.length > 2) {
File sourceFile = new File(Main.wd, cmd[1]);
File destinationFile = new File(Main.wd, cmd[2]);
try (FileInputStream fis = new FileInputStream(sourceFile);
FileOutputStream fos = new FileOutputStream(destinationFile)) {
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
System.out.println("File copied successfully.");
} catch (IOException e) {
System.out.println("Error copying file: " + e.getMessage());
}
} else {
System.out.println("Insufficient arguments. Usage: cp <source> <destination>");
}
}
}