-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWelcome_Screen.java
More file actions
196 lines (173 loc) · 6.89 KB
/
Copy pathWelcome_Screen.java
File metadata and controls
196 lines (173 loc) · 6.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package first_project_java;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
public class Welcome_Screen
{
static String DIRECTORY;
static File java_project;
static String n;
static String filename;
public static void main(String[] args)
{
// TODO Auto-generated method stub
System.out.println("************* Welocome To My Application ************* ");
MainMenu();
}
public static void MainMenu()
{
System.out.println(" 1. View All Files");
System.out.println(" 2. Create a new File");
System.out.println(" 3. Search File");
System.out.println(" 4. Delete File");
System.out.println(" 5. Exit");
menu();
}
public static void menu()
{
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("Please enter your choice : ");
int choice = scan.nextInt();
switch(choice)
{
case 1:
viewFiles();
goback();
break;
case 2:
createNew();
goback();
break;
case 3:
searchFile();
goback();
break;
case 4:
deleteFile();
goback();
break;
case 5:
exit();
goback();
break;
default :
System.out.println("please enter a only 1,2,3 or 4");
goback();
}
}
static void goback()
{
// TODO Auto-generated method stub
System.out.println("Do you want to conytinue Press - yes and for exit press - No" );
Scanner sc = new Scanner(System.in);
n = sc.next();
if(n.equalsIgnoreCase("yes"))
{
MainMenu();
}
if(n.equalsIgnoreCase("No"))
{
exit();
}
else
{
System.out.println("please enter a valid input 0 or 1 ! ");
goback();
}
}
static void exit()
{
// TODO Auto-generated method stub
System.exit(0);
}
static void deleteFile()
{
Scanner spfile = new Scanner(System.in);
System.out.println("Enter a file name (without .txt (a.txt) put only a) : ");
String Newfilename = spfile.next();
File f= new File("D:\\\\java_project\\\\"+Newfilename+".txt "); //file to be delete
if(f.delete()) //returns Boolean value
{
System.out.println(f.getName() + " deleted"); //getting and printing the file name
}
else
{
System.out.println("failed");
}
}
static void searchFile( )
{
// TODO Auto-generated method stub
File directoryPath = new File("D:\\java_project");
File filesList[] = directoryPath.listFiles();
Scanner file = new Scanner(System.in);
System.out.println("Enter a file name");
String newfilename = file.next();
File f = new File( "D:\\java_project\\"+file+".txt");
boolean fileound=false;
for(File file1:filesList)
{
File tempFile = new File("D:\\java_project\\"+newfilename+".txt");
boolean exists = tempFile.exists();
if(exists)
{
fileound = true;
}
else {
}
}
if(fileound)
{
System.out.println("File found");
}
else
{
System.out.println("File not found");
}
}
static void createNew()
{
// TODO Auto-generated method stub
boolean flag = false;
Scanner file = new Scanner(System.in);
System.out.println("Enter a new file name");
String newfilename = file.next();
File createfile = new File("D:\\java_project\\"+newfilename+".txt");
try
{
flag = createfile.createNewFile();
System.out.println("flag value" + flag);
if(!flag)
{
System.out.println("Same file name already exisy" + createfile.getPath());
}
else
{
System.out.println("file is created sucessfully" + createfile.getPath() + " created ");
}
}
catch (IOException ioe)
{
System.out.println("Error while Creating File in Java" + ioe);
}
}
static void viewFiles()
{
// TODO Auto-generated method stub
File directoryPath = new File("D:\\java_project");
File filesList[] = directoryPath.listFiles();
System.out.println("List of files and directories in the specified directory:");
for(File file : filesList)
{
System.out.println("File name: "+file.getName());
System.out.println("File path: "+file.getAbsolutePath());
System.out.println("Size :"+file.getTotalSpace());
System.out.println("last time file is modified :"+new Date(file.lastModified()));
System.out.println(" ");
}
}
}