|
| 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 | +} |
0 commit comments