Skip to content

Commit 05d7e29

Browse files
committed
Final commit
0 parents  commit 05d7e29

File tree

12 files changed

+292
-0
lines changed

12 files changed

+292
-0
lines changed

.idea/.gitignore

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/description.html

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/project-template.xml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

File Explorer.iml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
12+
Binary file not shown.
Binary file not shown.

src/com/company/FileExplorer.java

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
package com.company;
2+
import java.io.File;
3+
import java.io.IOException;
4+
import java.util.Scanner;
5+
6+
public class FileExplorer {
7+
public String parentDirectoryLocation;
8+
public String currentDirectoryLocation ;
9+
public String imgCurrentDirectoryLocation=">>File Explorer>";
10+
protected String newFileName;
11+
private String lastEnteredLoc[]=new String[10];
12+
private int top=-1;
13+
private Scanner fileInputs=new Scanner(System.in);
14+
FileExplorer(String parentDirectoryLocation){
15+
this.parentDirectoryLocation=parentDirectoryLocation;
16+
this.currentDirectoryLocation=parentDirectoryLocation;
17+
pushLocation("noValueTillNow");
18+
File newDirectory=new File(currentDirectoryLocation+"\\"+"test");
19+
newDirectory.mkdirs();
20+
if(newDirectory.exists())
21+
System.out.println("Ready...");
22+
else
23+
System.out.println("some error occured");
24+
}
25+
private void pushLocation(String data) {
26+
if(top>=9) {
27+
return;
28+
}else {
29+
top++;
30+
lastEnteredLoc[top]=data;
31+
}
32+
}
33+
private String popLocation() {
34+
if(top<0) {
35+
return "noValueTillNow" ;
36+
}else {
37+
String x=lastEnteredLoc[top];
38+
top--;
39+
return x;
40+
}
41+
}
42+
private String peepLocation() {
43+
if(top<0) {
44+
return "noValueTillNow" ;
45+
}else {
46+
String x=lastEnteredLoc[top];
47+
return x;
48+
}
49+
}
50+
public void fileStructure(String tempParentDirectory) {
51+
File directory=new File(tempParentDirectory);
52+
//get all file from directory
53+
File[] fileList=directory.listFiles();
54+
for(File fileElement : fileList) {
55+
if(fileElement.isFile())
56+
System.out.println(" >>"+fileElement.getAbsolutePath().substring(20+System.getProperty("user.name").length(),fileElement.getAbsolutePath().length()));
57+
else if(fileElement.isDirectory()) {
58+
System.out.println("dir>>"+fileElement.getAbsolutePath().substring(19+System.getProperty("user.name").length(),fileElement.getAbsolutePath().length()));
59+
fileStructure(fileElement.getAbsolutePath());
60+
}
61+
}
62+
}
63+
public String selectFileDirectory() {
64+
System.out.print(">");
65+
String enteredLoc=fileInputs.nextLine();
66+
File directory=new File(currentDirectoryLocation);
67+
File[] fileList=directory.listFiles();
68+
for(File fileElement : fileList) {
69+
if(enteredLoc.equals(fileElement.getName())) {
70+
pushLocation(enteredLoc); //to store last value of enteredLoc
71+
currentDirectoryLocation+="\\"+enteredLoc;
72+
imgCurrentDirectoryLocation+=enteredLoc+">";
73+
return imgCurrentDirectoryLocation;
74+
}
75+
}
76+
System.out.println("No such File or Folder");
77+
return "Invalid File Location";
78+
}
79+
//method to go back location
80+
public String bselectFileDirectory() {
81+
if(peepLocation().equals("noValueTillNow")) {
82+
System.out.println("Cannot be take back - Invalid Input");
83+
return "Cannot be take back - Invalid Input";
84+
}
85+
currentDirectoryLocation=removeLastChars(currentDirectoryLocation,peepLocation().length()+1);
86+
//System.out.println(currentDirectoryLocation);
87+
imgCurrentDirectoryLocation=removeLastChars(imgCurrentDirectoryLocation,peepLocation().length()+1);
88+
popLocation();
89+
return imgCurrentDirectoryLocation;
90+
}
91+
//method to reset location to parent directory
92+
public String rselectFileDirectory() {
93+
currentDirectoryLocation=parentDirectoryLocation;
94+
imgCurrentDirectoryLocation=">>Contact App>";
95+
top=0;
96+
return imgCurrentDirectoryLocation;
97+
}
98+
//method to remove last characters
99+
private String removeLastChars(String str,int noChar) {
100+
return str.substring(0, str.length() - noChar);
101+
}
102+
public void createNewDirectory(){
103+
System.out.print("Create a new Directory \nEnter name of Directory >>");
104+
String enteredDirectoryName =fileInputs.nextLine();
105+
File newDirectory=new File(currentDirectoryLocation+"\\"+enteredDirectoryName);
106+
boolean created=newDirectory.mkdirs();
107+
if(created)
108+
System.out.println("Directory successfully created at path :\n"+newDirectory.getPath());
109+
else if(newDirectory.exists())
110+
System.out.println("Directory already exist at path :\n"+newDirectory.getPath());
111+
else System.out.println("Unable to Create Directory");
112+
}
113+
public void createNewFile() throws IOException{
114+
System.out.print("Create a new File \nEnter name of File >>");
115+
String enteredFileName =fileInputs.next();
116+
newFileName=currentDirectoryLocation+"\\"+enteredFileName;
117+
File newFile=new File(newFileName);
118+
boolean created=newFile.createNewFile();
119+
if(created)
120+
System.out.println("File successfully created at path :\n"+newFile.getPath());
121+
else if(newFile.exists())
122+
System.out.println("File already exist at path :\n"+newFile.getPath());
123+
else System.out.println("Unable to Create File");
124+
}
125+
public String deleteFileFolder(String tempCurrentDirectory) {
126+
if(tempCurrentDirectory==parentDirectoryLocation||peepLocation().equals("noValueTillNow")) {
127+
return "Parent Folder cannot be deleted";
128+
}
129+
File directory=new File(tempCurrentDirectory);
130+
//get all file from directory
131+
if(directory.isFile()) {
132+
directory.delete();
133+
System.out.println(currentDirectoryLocation);
134+
currentDirectoryLocation=removeLastChars(currentDirectoryLocation,peepLocation().length()+1);
135+
System.out.println(currentDirectoryLocation);
136+
imgCurrentDirectoryLocation=removeLastChars(imgCurrentDirectoryLocation,peepLocation().length()+1);
137+
popLocation();
138+
}else {
139+
deleteFolder(tempCurrentDirectory);
140+
directory.delete();
141+
System.out.println(currentDirectoryLocation);
142+
currentDirectoryLocation=removeLastChars(currentDirectoryLocation,peepLocation().length()+1);
143+
System.out.println(currentDirectoryLocation);
144+
imgCurrentDirectoryLocation=removeLastChars(imgCurrentDirectoryLocation,peepLocation().length()+1);
145+
popLocation();
146+
}
147+
return "file or folder deleted successfully";
148+
}
149+
private void deleteFolder(String tempCurrentDirectory) {
150+
File directory=new File(tempCurrentDirectory);
151+
File[] fileList=directory.listFiles();
152+
for(File fileElement : fileList) {
153+
if(fileElement.isFile())
154+
fileElement.delete();
155+
else if(fileElement.isDirectory()) {
156+
deleteFolder(fileElement.getAbsolutePath());
157+
fileElement.delete();
158+
}
159+
}
160+
}
161+
162+
public void listFilesFolder() {
163+
File directory=new File(currentDirectoryLocation);
164+
//get all file from directory
165+
File[] fileList=directory.listFiles();
166+
for(File fileElement : fileList) {
167+
if(fileElement.isFile())
168+
System.out.println(fileElement.getName());
169+
else if(fileElement.isDirectory()) {
170+
System.out.println(fileElement.getName()+"(folder)");
171+
}
172+
}
173+
}
174+
}

src/com/company/Main.java

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.company;
2+
import java.io.File;
3+
import java.io.IOException;
4+
import java.util.Scanner;
5+
6+
public class Main {
7+
8+
public static void main(String[] args) {
9+
// write your code here
10+
FileExplorer fo= new FileExplorer(System.getProperty("user.home")+"\\Documents\\File Explorer");
11+
Scanner mainInputs=new Scanner(System.in);
12+
String status;
13+
System.out.println("######################################"
14+
+ "\n######### File Explorer #########"
15+
+ "\n######################################");
16+
System.out.println("**************************************"
17+
+ "\nlistcmd : Show Commands"
18+
+ "\ncontent : Show structure of file"
19+
+ "\nls : Show content of folder"
20+
+ "\nselect : Select Directory and Files"
21+
+ "\n--back : previous Directory"
22+
+ "\n--reset : back to parent Directory"
23+
+ "\nmakedir : Create Directory"
24+
+ "\nmakefile : Create File"
25+
+ "\ndelete : Delete File or Folder"
26+
+ "\nexit : Exit"
27+
+ "\n**************************************");
28+
do {
29+
System.out.print(fo.imgCurrentDirectoryLocation);
30+
status=mainInputs.next();
31+
if ("listcmd".equals(status)) {
32+
System.out.println("**************************************"
33+
+ "\nlistcmd : Show Commands"
34+
+ "\ncontent : Show structure of file"
35+
+ "\nls : Show content of folder"
36+
+ "\nselect : Select Directory and Files"
37+
+ "\n--back : previous Directory"
38+
+ "\n--reset : back to parent Directory"
39+
+ "\nmakedir : Create Directory"
40+
+ "\ndelete : Delete File or Folder"
41+
+ "\nmakefile : Create File"
42+
+ "\nexit : Exit"
43+
+ "\n**************************************");
44+
} else if ("content".equals(status)) {
45+
fo.fileStructure(fo.parentDirectoryLocation);
46+
} else if ("select".equals(status)) {
47+
fo.selectFileDirectory();
48+
} else if ("back".equals(status)) {
49+
fo.bselectFileDirectory();
50+
} else if ("reset".equals(status)) {
51+
fo.rselectFileDirectory();
52+
} else if ("makedir".equals(status)) {
53+
fo.createNewDirectory();
54+
} else if ("makefile".equals(status)) {
55+
try {
56+
fo.createNewFile();
57+
} catch (IOException e) {
58+
// TODO Auto-generated catch block
59+
e.printStackTrace();
60+
}
61+
} else if ("delete".equals(status)) {
62+
System.out.println(fo.deleteFileFolder(fo.currentDirectoryLocation));
63+
} else if ("ls".equals(status)) {
64+
fo.listFilesFolder();
65+
} else {
66+
System.out.println("Command not defined");
67+
}
68+
}while(!status.equals("exit"));
69+
mainInputs.close();
70+
}
71+
}

0 commit comments

Comments
 (0)