public void copy(String frompath, String topath) throws IOException { String from = new File(currentDiskPath + "\\" + frompath).getAbsolutePath(); String fileName = from.substring(from.lastIndexOf("\\"), from.length()); String to = new File(currentDiskPath + "\\" + topath + "\\" + fileName).getAbsolutePath(); if (!(new File(currentDiskPath + "\\" + topath)).isDirectory() || !(new File(from).exists())) { System.out.println("Unul din fisiere este inexistent"); takePath(); return; } Files.copy(Paths.get(from), Paths.get(to), REPLACE_EXISTING); takePath(); }
// navigam up cu un director public void up() { // Nu putem face up din disk if (currentPath.equals(currentDiskPath)) return; int lastIndex = currentPath.lastIndexOf("\\"); currentPath = currentPath.substring(0, lastIndex); takePath(); }
public void rename(String frompath, String topath) throws PhileNotFoundException, FileSystemException { File oldname = new File(currentPath + "\\" + frompath); boolean newname = oldname.renameTo(new File(currentPath + "\\" + topath)); if (!newname) { throw new FileSystemException(currentPath + "\\" + topath); } takePath(); }
// formateaza un disk public void format() { File root = new File(currentDiskPath); File[] listOfFiles = root.listFiles(); for (int i = 0; i < listOfFiles.length; i++) { delete(listOfFiles[i]); } System.out.println("Format succesfull!"); takePath(); }
public void ls() { // OVERLOADING :(((( File folder = new File(currentPath); File[] listOfFiles = folder.listFiles(); for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile()) { System.out.println("File: " + listOfFiles[i].getName()); } else if (listOfFiles[i].isDirectory()) { System.out.println("Directory: " + listOfFiles[i].getName()); } } takePath(); }
public void mkdir(String newDirectoryName) { try { File newDirectory = new File(currentPath + "\\" + newDirectoryName); if (newDirectory.exists()) { System.out.println("Unable to create " + newDirectory.getAbsolutePath()); } else { newDirectory.mkdir(); System.out.println("Directory" + newDirectoryName + "created"); } } finally { takePath(); } }
public void allDisks() { try { System.out.println("Discurile existente in sistem:"); String line; int i = 0; BufferedReader br = new BufferedReader(new FileReader("disks.txt")); while ((line = br.readLine()) != null) { i++; System.out.println("" + i + ":" + line); } } catch (Exception e) { } finally { takePath(); } }
// afiseaza lista comenzilor public void help() { System.out.println("==============COMMANDS LIST=============="); System.out.println("newDisk --->NEW DISK "); System.out.println("allDisks --->EXISTENT DISKS ON THE SYSTEM"); System.out.println("load --->LOAD A DISK "); System.out.println("shutdown --->SHUT DOWN SYSTEM "); System.out.println("mkdir --->CREATE A NEW DIRECTORY "); System.out.println("mkfile --->CREATE A NEW FILE "); System.out.println("chdir --->CHANGE DIRECTORY "); System.out.println("help --->HELP INFORMATIONS "); System.out.println("move --->move x to y directory "); System.out.println("ls --->List the current directory "); System.out.println("up --->Up one level "); takePath(); }
// schimba directorul curent public void chdir(String path) { try { File currentDirectory = new File(currentPath); String[] allDirectories = currentDirectory.list(); boolean directoryExist = false; for (String item : allDirectories) { if (item.equals(path)) { directoryExist = true; } } if (directoryExist == true) currentPath = currentPath + "\\" + path; else System.out.println("The directory doesn't exists"); } finally { takePath(); } }
// deschide fereastra editorului pentru fisierul openPhilePath public void edit(String entry) throws PhileNotOpenException { try { String targetPath = currentPath + "\\" + entry; File file = new File(targetPath); if (!file.exists()) { System.out.println("Fisierul nu exista"); return; } Desktop dk = Desktop.getDesktop(); // Open a file dk.edit(file); } catch (Exception e) { } finally { takePath(); } }
// copiaza frompath la topath public void move(String frompath, String topath) throws PhileNotFoundException { File from = new File(currentDiskPath + "\\" + frompath); String fullFromPath = from.getAbsolutePath(); String fileName = fullFromPath.substring(fullFromPath.lastIndexOf("\\"), fullFromPath.length()); File to = new File(currentDiskPath + "\\" + topath + fileName); if (from.exists() && to.getParentFile().isDirectory()) { from.renameTo(to); } else { System.out.println("Nu exista calea indicata"); } takePath(); }
// creaza si deschide fisierul cu numele specificat public void mkfile(String fileName) { try { File newFile = new File(currentPath + "\\" + fileName); if (newFile.exists()) { System.out.println("Unable to create " + newFile.getAbsolutePath()); } else { newFile.createNewFile(); System.out.println("Directory" + fileName + "created"); openFile(fileName); } } catch (Exception e) { e.printStackTrace(); } finally { takePath(); } }
// creaza un nou disk si un nou sistem de fisiere pe acesta public void newDisk(String diskName) { try { File newDisk = new File(System.getProperty("user.dir").toString() + "/" + diskName); if (newDisk.exists()) { System.out.println("Unable to create " + newDisk.getAbsolutePath()); } else { newDisk.mkdirs(); System.out.println("New disk created:" + diskName); addDiskRefernce(diskName); currentDiskPath = System.getProperty("user.dir").toString() + "\\" + diskName; currentPath = currentDiskPath; } } finally { takePath(); } }
// incarca un disk existent de pe HDD; trebuie folosit un obiect de tip FileDialog public void load(String diskName) { try { String line; boolean ok = false; BufferedReader br = new BufferedReader(new FileReader("disks.txt")); while ((line = br.readLine()) != null) { if (line.equals(diskName)) ok = true; } if (ok == true) { System.out.println("Discul " + diskName + " a fost incarcat"); currentDiskPath = System.getProperty("user.dir").toString() + "\\" + diskName; currentPath = currentDiskPath; } else System.out.println("Discul nu exista!"); } catch (Exception e) { } finally { takePath(); } }
// returneaza true daca entry reprezinta un director si false in caz contrar public boolean isDirectory(String entry) { try { String targetPath = currentPath + "\\" + entry; File file = new File(targetPath); if (!file.exists()) System.out.println("Fisierul nu exista"); if (file.isDirectory()) { System.out.println(entry + " este un director"); return true; } else { System.out.println(entry + " nu este un director"); return false; } } catch (Exception e) { return false; } finally { takePath(); } }
public static void main(String[] args) { Console c = new Console(); c.copyright(); System.out.print(currentPath); Scanner sc = new Scanner(System.in); String input; while (true) { input = sc.nextLine(); if (input.equals("")) { c.takePath(); continue; } if (input.equals("exit")) { System.out.println("Consola a fost inchisa!"); break; } runtimeCall(input, c); } }
// sterge un fisier/director cu numele name public void delete(String name) throws PhileNotFoundException { File targetFile = new File(currentPath + "\\" + name); if (targetFile.exists()) delete(targetFile); takePath(); }