-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileIO.java
39 lines (32 loc) · 1.39 KB
/
FileIO.java
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
37
38
39
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
public class FileIO {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please type the source file path...");
String sourceFilePath = input.nextLine();
System.out.println();
System.out.println("Please type the destination path...");
String destDirPath = input.nextLine();
System.out.println();
input.close();
File sourceFile = new File(sourceFilePath);
String fileName = sourceFile.getName();
String destFilePath = destDirPath + "/" + fileName;
try (FileInputStream fileInputStream = new FileInputStream(sourceFilePath);
FileOutputStream fileOutputStream = new FileOutputStream(destFilePath)) {
byte [] buffer = new byte[1024];
int bytesRead;
System.out.println("Copying file...\n");
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
}
System.out.println("File copied successfully.");
} catch (IOException e) {
System.out.println("Error copying file: " + e.getMessage());
}
}
}