-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloudFileManager.java
More file actions
80 lines (73 loc) · 2.89 KB
/
Copy pathCloudFileManager.java
File metadata and controls
80 lines (73 loc) · 2.89 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.io.*;
import java.nio.file.*;
import java.util.Scanner;
public class CloudFileManager {
private static final String CLOUD_DIR = "cloud_storage/"; // Simulated cloud folder
public static void main(String[] args) {
// Create cloud directory if it doesn’t exist
File dir = new File(CLOUD_DIR);
if (!dir.exists()) dir.mkdir();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\nCloud File Manager");
System.out.println("1. Upload File");
System.out.println("2. Download File");
System.out.println("3. List Files");
System.out.println("4. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Clear buffer
switch (choice) {
case 1: uploadFile(scanner); break;
case 2: downloadFile(scanner); break;
case 3: listFiles(); break;
case 4: System.out.println("Exiting..."); return;
default: System.out.println("Invalid option!");
}
}
}
private static void uploadFile(Scanner scanner) {
System.out.print("Enter file path to upload: ");
String sourcePath = scanner.nextLine();
File sourceFile = new File(sourcePath);
if (!sourceFile.exists()) {
System.out.println("File not found!");
return;
}
try {
Files.copy(sourceFile.toPath(), Paths.get(CLOUD_DIR + sourceFile.getName()),
StandardCopyOption.REPLACE_EXISTING);
System.out.println("File uploaded successfully!");
} catch (IOException e) {
System.out.println("Upload failed: " + e.getMessage());
}
}
private static void downloadFile(Scanner scanner) {
System.out.print("Enter file name to download: ");
String fileName = scanner.nextLine();
File sourceFile = new File(CLOUD_DIR + fileName);
if (!sourceFile.exists()) {
System.out.println("File not found in cloud!");
return;
}
try {
Files.copy(sourceFile.toPath(), Paths.get("downloaded_" + fileName),
StandardCopyOption.REPLACE_EXISTING);
System.out.println("File downloaded as downloaded_" + fileName);
} catch (IOException e) {
System.out.println("Download failed: " + e.getMessage());
}
}
private static void listFiles() {
File dir = new File(CLOUD_DIR);
File[] files = dir.listFiles();
if (files == null || files.length == 0) {
System.out.println("No files in cloud storage.");
return;
}
System.out.println("Files in cloud:");
for (File file : files) {
System.out.println("- " + file.getName());
}
}
}