-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserFiles.java
More file actions
150 lines (105 loc) · 3.17 KB
/
Copy pathUserFiles.java
File metadata and controls
150 lines (105 loc) · 3.17 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
package FsdProject;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class UserFiles {
Path path;
//To get the file Directory
public void createDirectory() {
this.path = Paths.get("./Files/UserFiles");
try {
Files.createDirectories(path); System.out.println("Directory created successfully");
} catch (IOException e) {
System.out.println("Failed to create the directory"
+e.getMessage());
e.printStackTrace();
}
}
public Path getDirectoryPath() {
return this.path;
}
//Add a user specified file to the application
public void createuserFile() {
try (Scanner sc = new Scanner(System.in)) {
System.out.println("Enter the File Name you want to create");
String filename= sc.next();
Path newfilepath =Paths.get(this.path + "/" + filename);
try { Files.createFile(newfilepath);
System.out.println("File Created Successfully");
}
catch(FileAlreadyExistsException e) {
System.out.println("The file you want to create already exists");
this.createuserFile();
}
catch(IOException e) {
System.out.println("Failed to create File" +e.getMessage());
}
}
}
//To list all the files added to the directory
public void listofFiles() {
String dir = this.path.toString();
File[] listOfFiles = new File(dir).listFiles();
for(File file: listOfFiles) {
if(file.isDirectory()) { System.out.println(file.getName());
}
else if(file.isFile()) { System.out.println(file.getName());
}
}
}
//To search a user specified file from the application
public void searchFile() {
try (Scanner sc = new Scanner(System.in)) {
System.out.println("Enter the File Name you want to search");
String FiletoSearch = sc.next();
Path path = Paths.get(this.path + "/" + FiletoSearch);
if(Files.exists(path)) {
if(Files.isRegularFile(path)) {
System.out.println("Files Exists");
}
if(Files.isDirectory(path)) {
System.out.println("File Exists but it is a Directory");
}
}
else {
System.out.println("File doesnot Exists");
}
}
}
//To delete User specified File from the application
public void deleteuserFile() {
try (Scanner sc = new Scanner(System.in)) {
System.out.println("Enter the File Name you want to delete");
String FileToDelete = sc.next();
try {
Files.delete(Paths.get(this.path + "/" + FileToDelete));
System.out.println("File Deleted Successfully");
}
catch(NoSuchFileException e) {
System.out.println("File doesnot exists!!!Enter new File Name to delete");
}
catch(IOException e) {
e.printStackTrace();
}
}
}
//To List all the Files in Ascending Order
public void listFilesinAscendingOrder() { String dir = this.path.toString();
File[] listOfFiles = new File(dir).listFiles();
List<File> listofFile = Arrays.asList(listOfFiles);
Collections.sort(listofFile);
Iterator<File> it = listofFile.iterator();
while(it.hasNext()) {
System.out.println(it.next().getName());
}
}
}