-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase.java
More file actions
117 lines (94 loc) · 4.41 KB
/
Base.java
File metadata and controls
117 lines (94 loc) · 4.41 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Base {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String flag = "1";
while(flag.contains("1"))
{
System.out.println("*****************************************************");
System.out.println("Welcome to LockedMe Portal...What would you like to do ?");
System.out.println("*****************************************************");
System.out.println("Enter: \n 1- To Retrieve the file name in Ascending Order \n 2- To Add the specified file Name \n 3- To Delete the specified file \n 4- To Search the specified file Name \n 5- To exit the application");
String a = sc.next();
performOperation(a);
System.out.println("Do you want to continue ? Enter 1 to continue..or enter anything else to exit");
flag = sc.next();
}
System.out.println("Thank you..Exiting now...!!");
}
private static void performOperation(String a) throws IOException {
Scanner s = new Scanner(System.in);
try
{
switch(a)
{
case "1":
System.out.println("Enter the directory Path where you want to perform Search(e.g C://Anand Backup)");
String c = s.nextLine();
//Creating a File object for directory
File directoryPath = new File(c);
//List of all files and directories
String contents[] = directoryPath.list();
System.out.println("List of files and directories(in Alphabetical Order) in the specified directory:");
for(int i=0; i<contents.length; i++) {
System.out.println(i+1 + ") "+ contents[i]);
}
break;
case "2":
System.out.println("Enter the file name you want to add...");
String fileName = s.nextLine();
System.out.println("Enter the Directory where you want to add the above file...(e.g C://Anand Backup)");
String directory = s.nextLine();
// creating the file based upon directory and fileName entered.
File file = new File(directory + "//"+ fileName);
if (!file.exists()) {
file.createNewFile(); // Create a new file if not exists.
System.out.println("File -" + fileName + " Added successfully ..!!");
}
else
System.out.println("File already exists..!!Please select a unique name for the file or delete the older file to add with the same name");
break;
case "3":
System.out.println("Enter the file name you want to delete...");
String DeletefileName = s.nextLine();
System.out.println("Enter the Directory from where you want to delete the above file...(e.g C://Anand Backup)");
String DeleteDirectory = s.nextLine();
// creating the file based upon directory and fileName entered.
File fileDelete = new File(DeleteDirectory + "//"+ DeletefileName);
if (!fileDelete.exists()) {
System.out.println("File is already Deleted..!!");
}
else
fileDelete.delete(); // Delete the file if exists.
System.out.println("File " + DeletefileName + " deleted successfully..!!");
break;
case "4":
System.out.println("Enter the file name you want to search...");
String SearchfileName = s.nextLine();
System.out.println("Enter the Directory path where you want to search the above file...(e.g C://Anand Backup)");
String SearchDirectory = s.nextLine();
// creating the file based upon directory and fileName entered.
File fileSearch = new File(SearchDirectory + "//"+ SearchfileName);
if (!fileSearch.exists()) {
System.out.println("File does not exists..!!");
}
else
System.out.println("File " + SearchfileName + " exists in the provided directory..!!");
break;
case "5":
System.out.println("Terminating the session");
break;
default:
System.out.println("Invalid Input...Pls enter any value out of 1 - 5");
break;
}
}
catch(NullPointerException e)
{
System.out.println("Please enter a valid directory path...!!"+e.getStackTrace());
}
}
}