Simple file manager app

Very simple console based file manager application. Just an exercise for file system handling.

Created on: Jan 23, 2017.

Main.java

1:  import java.util.Scanner;  
2:    
3:  public class Main {  
4:    
5:    public static void main(String[] args) {  
6:        
7:      System.out.println("LIST, INFO, CREATE_DIR, RENAME, COPY, MOVE, DELETE");  
8:      System.out.print("Select the desired operation: ");  
9:      Scanner s = new Scanner (System.in);  
10:      String input = s.nextLine();  
11:        
12:      try {   
13:        Operacije op = Operacije.valueOf(input);  
14:        System.out.print("Operation accepted ");  
15:        switch (op){  
16:          case LIST :  
17:            Manipulacije.list();  
18:            break;  
19:          case INFO :  
20:            Manipulacije.info();  
21:            break;  
22:          case CREATE_DIR :  
23:            Manipulacije.createDir();  
24:            break;  
25:          case RENAME :  
26:            Manipulacije.rename();  
27:            break;  
28:          case COPY :  
29:            Manipulacije.copy();  
30:            break;  
31:          case MOVE :  
32:            Manipulacije.move();  
33:            break;  
34:          case DELETE :  
35:            Manipulacije.delete();  
36:            break;  
37:          default :  
38:            System.out.println("*");  
39:            break;  
40:        }  
41:      } catch (Exception e) {   
42:        System.out.println("Operation does not exist." + "\nTry again.");   
43:       }  
44:        
45:    }  
46:      
47:  }  
48:    


Operacije.java

1:  public enum Operacije {  
2:    LIST,INFO,CREATE_DIR,RENAME,COPY,MOVE,DELETE;  
3:  }  


Manipulacije.java

1:  import java.io.File;  
2:  import java.io.FileInputStream;  
3:  import java.io.FileOutputStream;  
4:  import java.io.IOException;  
5:  import java.time.Instant;  
6:  import java.time.LocalDateTime;  
7:  import java.time.ZoneId;  
8:  import java.time.format.DateTimeFormatter;  
9:  import java.util.Scanner;  
10:    
11:  public class Manipulacije {  
12:      
13:    public static String pathInput() {  
14:      System.out.print("Input desired path: ");  
15:      Scanner u = new Scanner (System.in);  
16:      String input = u.nextLine();  
17:      return input;  
18:    }    
19:    public static void list() {  
20:      System.out.println("Folder view");  
21:      File path = new File (pathInput());  
22:      if (path.exists() && path.isDirectory()) {  
23:        String[] strings = path.list();  
24:        for(int i=0; i<strings.length; i++)  
25:          System.out.println(strings[i]);  
26:      } else  
27:        System.out.println("The selected folder does not exist.");  
28:    }  
29:    public static void info() {  
30:      System.out.println("Info view");  
31:      File path = new File (pathInput());  
32:      if (path.exists()) {  
33:        System.out.println("Name: " + path.getName());  
34:        System.out.println("Path: " + path.getPath());  
35:        System.out.println("Size: " + path.length() + " bytes");  
36:        System.out.println("Creation date: ");  
37:        Instant inst = Instant.ofEpochMilli(path.lastModified());  
38:        LocalDateTime dT = LocalDateTime.ofInstant(inst, ZoneId.systemDefault());  
39:        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd. MMM yyyy.");   
40:        System.out.println("Date of the last change: " + dT.format(formatter));  
41:      } else   
42:        System.out.println("Selected file/folder does not exist.");  
43:    }  
44:    public static void createDir() {  
45:      System.out.println("Folder creating");  
46:      File path = new File (pathInput());  
47:      if(!path.exists()) {  
48:        path.mkdir();  
49:        System.out.println("Folder " + path.getName() + " Successfully created.");  
50:      } else  
51:        System.out.println("Folder can not be created " + path.getName());  
52:    }  
53:    public static void rename() {  
54:      System.out.println("Rename");  
55:      File oldPath = new File (pathInput());  
56:      File newPath = new File (pathInput());  
57:      if(!oldPath.exists())  
58:        System.out.println(oldPath.getName() + " does not exist.");  
59:      if(newPath.exists())  
60:        System.out.println(newPath.getName() + " already exists.");  
61:      if(oldPath.renameTo(newPath))  
62:        System.out.println("Rename successfully executed.");  
63:      else  
64:        System.out.println("Rename has failed");  
65:    }  
66:    public static void copy() {  
67:      System.out.println("Copy");  
68:      File oldPath = new File (pathInput());  
69:      File newPath = new File (pathInput());  
70:      if (oldPath.exists() && oldPath.isDirectory()) {       
71:        newPath.mkdir();  
72:        for (String f : oldPath.list()) {  
73:          File newFile1 = new File(oldPath, f);  
74:          File newFile2 = new File(newPath, f);  
75:          try(FileInputStream inStr = new FileInputStream(newFile1);  
76:            FileOutputStream outStr = new FileOutputStream(newFile2);) {  
77:              byte[] buffer = new byte[1024];  
78:              int length;  
79:              while((length=inStr.read(buffer))>0) {  
80:                outStr.write(buffer,0,length);  
81:              }  
82:          } catch (IOException e) {  
83:            System.out.println("Copying folder " + oldPath.getName() +  
84:                " has failed.");  
85:          }  
86:        }  
87:        System.out.println("Folder " + oldPath.getName() + " successfully copied.");  
88:      }  
89:      if(oldPath.exists() && oldPath.isFile()) {  
90:        try (FileInputStream inS = new FileInputStream(oldPath);  
91:           FileOutputStream ouS = new FileOutputStream(newPath);) {  
92:          byte[] buffer = new byte[1024];  
93:          int len;  
94:          while((len=inS.read(buffer))>0) {  
95:            ouS.write(buffer,0,len);  
96:          }  
97:          System.out.println("File " +oldPath.getName() + " successfully copied.");  
98:        } catch (IOException e) {  
99:          System.out.println("Copying file " + oldPath.getName()+   
100:              " has failed.");  
101:        }  
102:      }  
103:    }    
104:    public static void delete() {  
105:      System.out.println("Delete");  
106:      File path = new File(pathInput());  
107:      System.out.print("Are you sure you want to delete " +path.getName()  
108:          +" ? y/n: ");  
109:        Scanner s1 = new Scanner(System.in);  
110:        String izbor = s1.nextLine();  
111:      switch (izbor) {  
112:        case "y" :  
113:          if(path.exists() && path.isDirectory()) {  
114:            if(path.length() != 0){  
115:              for(String x : path.list()){  
116:                File xFile = new File(path, x);  
117:                xFile.delete();  
118:              }  
119:            }  
120:            path.delete();  
121:            System.out.println("Folder " + path.getName() +   
122:                " successfully deleted.");  
123:          }  
124:          if(path.exists() && path.isFile()) {  
125:            path.delete();  
126:            System.out.println("File " + path.getName() +   
127:                " successfully deleted.");  
128:          }  
129:          break;  
130:        case "n" :  
131:          System.out.println(path.getName() + " not deleted.");  
132:          break;  
133:        default :  
134:          System.out.println("You should simply select y or n");  
135:          System.out.println(path.getName() + " not deleted.");  
136:          break;  
137:      }  
138:    }  
139:    public static void move() {  
140:      System.out.println("Move");  
141:      File oldPath = new File(pathInput());  
142:      File newPath = new File(pathInput());  
143:      if (oldPath.exists() && oldPath.isDirectory()) {       
144:        newPath.mkdir();  
145:        for (String f : oldPath.list()) {  
146:          File newFile1 = new File(oldPath, f);  
147:          File newFile2 = new File(newPath, f);  
148:          try(FileInputStream inStr = new FileInputStream(newFile1);  
149:            FileOutputStream outStr = new FileOutputStream(newFile2);) {  
150:              byte[] buffer = new byte[1024];  
151:              int length;  
152:              while((length=inStr.read(buffer))>0) {  
153:                outStr.write(buffer,0,length);  
154:              }  
155:          } catch (IOException e) {  
156:            System.out.println("Moving foldera " + oldPath.getName() +  
157:                " has failed.");  
158:          }  
159:        }  
160:        if(oldPath.length() != 0){  
161:              for(String x : oldPath.list()){  
162:                File xFile = new File(oldPath, x);  
163:                xFile.delete();  
164:              }  
165:            }  
166:            oldPath.delete();  
167:            System.out.println("Moving foldera " + oldPath.getName()   
168:              + " was successfully executed.");  
169:      }  
170:      if(oldPath.exists() && oldPath.isFile()) {  
171:        try (FileInputStream inS = new FileInputStream(oldPath);  
172:           FileOutputStream ouS = new FileOutputStream(newPath);) {  
173:          byte[] buffer = new byte[1024];  
174:          int len;  
175:          while((len=inS.read(buffer))>0) {  
176:            ouS.write(buffer,0,len);  
177:          }  
178:        } catch (IOException e) {  
179:          System.out.println("Moving file " + oldPath.getName()+   
180:              " has failed.");  
181:        }  
182:        oldPath.delete();  
183:        System.out.println("File " + oldPath.getName() + " successfully moved.");  
184:      }  
185:    }  
186:      
187:  }  

Comments

  1. Pozdrav Djordje,

    Svaka cast na svemu sto si uradio i na ovom lepom blogu.
    Cini mi se samo da sam primetio da nedostaje nesto u zadatku. Ne prikazuje datum kreiranja fajla.
    U Manipulacije.java kod " info " . Ucim, nisam ni blizu nekog znanja.
    Super su ti zadaci i blog. :)

    Pozdrav

    ReplyDelete
    Replies
    1. Upravu si. Svaka čast i hvala ti. Ja sam to skroz prevideo. :) Baš mi je drago ako ti ovi radovi pomažu u učenju. Ako mogu još nekako da ti pomognem slobodno reci. :) I hvala na lepim rečima.
      Sve najbolje

      Delete

Post a Comment