-
-
Save Viacheslav77/4f69210259fa16195547 to your computer and use it in GitHub Desktop.
Написать проект «Виртуальная файловая система». Каждый каталог и файл представлен одним объектом (Directory/File); должны поддерживатся связи между объектами; вся файловая структура хранится в одном файле. Обеспечить базовые ф-и для работы с ФС: добавление и удаление файлов и каталогов etc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package VirtualFileSysMy; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
public class GUI { | |
public static void mainMenu(VirtyalFileSystem obj) throws IOException { | |
System.out.println("\n The Virtyal File System is loaded\n"); | |
helpMenu(); | |
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); | |
try { | |
while (true) { | |
System.out.print("\n root: "); | |
String line = in.readLine(); | |
System.out.print(line + " "); | |
if (line.equals("help")) { | |
helpMenu(); | |
continue; | |
} | |
if (line.equals("exit")) { | |
in.close(); | |
return; | |
} | |
if (obj != null) { | |
String command = in.readLine(); | |
switch (line) { | |
case "dir": | |
obj.showAll(); | |
break; | |
case "mkdir": | |
System.out.println("Enter name of the directory"); | |
String name = in.readLine(); | |
obj.add(name); | |
obj.showAll(); | |
break; | |
case "del": | |
System.out.println("Enter directory or file name to delete"); | |
name = in.readLine(); | |
obj.remove(name); | |
obj.showAll(); | |
break; | |
case "exit": | |
in.close(); | |
return; | |
} | |
} else { | |
System.out.println("Unknown command"); | |
continue; | |
} | |
} | |
} finally { | |
in.close(); | |
} | |
} | |
public static void helpMenu() { | |
StringBuilder sb = new StringBuilder(); | |
sb.append("List of the command:\n\n"); | |
sb.append("List of the file - dir:\n"); | |
sb.append("Create directory - mkdir\n"); | |
sb.append("Delete file or directory - del\n"); | |
sb.append("Exit - exit\n"); | |
System.out.println(sb.toString()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package VirtualFileSysMy; | |
/*Написать проект «Виртуальная файловая система». Каждый каталог и файл представлен одним объектом (Directory/File); | |
должны поддерживатся связи между объектами; вся файловая структура хранится в одном файле. Обеспечить базовые ф-и | |
для работы с ФС: добавление и удаление файлов и каталогов etc. */ | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.ObjectInputStream; | |
import java.io.ObjectOutputStream; | |
public class Main { | |
public static void main(String[] args) throws Exception{ | |
VirtyalFileSystem root = new VirtyalFileSystem(); | |
File f = new File("d:\\1\\info.txt"); | |
if (f.isFile() && f.exists()) { | |
root = toDeserialize(f); | |
root.showAll(); | |
GUI.mainMenu(root); | |
root.toSerialize(f); | |
} else { | |
f.createNewFile(); | |
ShablonFiles(root); | |
GUI.mainMenu(root); | |
root.toSerialize(f); | |
System.out.println("______________"); | |
toDeserialize(f).showAll(); | |
} | |
} | |
public static VirtyalFileSystem toDeserialize(File path) throws Exception{ | |
ObjectInputStream obin=null; | |
VirtyalFileSystem obj=null; | |
try{ | |
obin = new ObjectInputStream(new FileInputStream(path)); | |
obj = (VirtyalFileSystem)obin.readObject(); | |
}catch(Exception ex){ | |
System.out.println(ex); | |
} | |
System.out.println(obj+" has been deserialized from "+path+"\n"); | |
obin.close(); | |
return obj; | |
} | |
public static void ShablonFiles(VirtyalFileSystem root) { | |
VirFile file1 = new VirFile("file1.txt"); | |
VirFile file2 = new VirFile("file2.txt"); | |
VirFile file3 = new VirFile("file3.txt"); | |
VirDir Direcktory1 = new VirDir("Direcktory1"); | |
VirDir Direcktory2 = new VirDir("Direcktory2"); | |
VirDir Direcktory3 = new VirDir("Direcktory3"); | |
root.addobj(Direcktory1); | |
root.addobj(file1); | |
Direcktory1.addobj(Direcktory2); | |
Direcktory1.addobj(file2); | |
Direcktory2.addobj(Direcktory3); | |
Direcktory3.addobj(file3); | |
root.addobj(file3); | |
System.out.println("______________"); | |
root.showAll(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package VirtualFileSysMy; | |
import java.util.Date; | |
class VirDir extends VirtyalFileSystem{ | |
private static final long serialVersionUID = 1L; | |
protected String name=""; | |
protected VirDir(String name){ | |
this.name = name; | |
this.isDir=true; // является директорией | |
d = new Date (System.currentTimeMillis()); | |
} | |
public String toString(){ | |
return this.name; | |
} | |
public String getDirecktory() { | |
return name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package VirtualFileSysMy; | |
import java.util.Date; | |
class VirFile extends VirtyalFileSystem{ | |
private static final long serialVersionUID = 1L;; | |
protected String name=""; | |
protected VirFile(String name){ | |
this.name = name; | |
this.isDir=false; //не является директорией | |
d = new Date (System.currentTimeMillis()); | |
} | |
public String toString(){ | |
return this.name; | |
} | |
public String getName() { | |
return name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package VirtualFileSysMy; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.ObjectInputStream; | |
import java.io.ObjectOutputStream; | |
import java.io.Serializable; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import VirtualFileSystemLesson8.Direcktory; | |
public class VirtyalFileSystem implements Serializable{ | |
private static final long serialVersionUID = 1L; | |
protected String name="Root"; | |
protected VirtyalFileSystem root=null; | |
protected ArrayList<VirtyalFileSystem> LIST = new ArrayList<VirtyalFileSystem>(); | |
protected boolean isDir=true; | |
protected Date d; | |
public String toString(){ | |
return this.name; | |
} | |
protected String getPath(){ | |
String p = "\\"+this; | |
if(this.root==null) | |
return p; | |
else{ | |
p=root.getPath()+p; | |
} | |
return p; | |
} | |
protected void remove(String name) { | |
VirtyalFileSystem o = find1(name); | |
LIST.remove(o); | |
if (o==null){ | |
System.out.println("Direcktory "+ name +" can't be removed."+this+"\n"); | |
return; | |
} | |
else | |
System.out.println("Direcktory "+ name +" has been removed from "+this+"\n"); | |
} | |
protected VirtyalFileSystem find1(String name){ | |
for(VirtyalFileSystem o : this.LIST){ | |
if (o.toString().equals(name)){ | |
return o; | |
} | |
} | |
return null; | |
} | |
protected boolean find(VirtyalFileSystem obj){ | |
for(VirtyalFileSystem o : this.LIST){ | |
if (o.toString().equals(obj.toString())){ | |
return true; | |
} | |
} | |
return false; | |
} | |
protected void add(String name){ | |
VirtyalFileSystem o = find1(name); | |
if (o==null){ | |
VirtyalFileSystem obj = new VirDir(name); | |
obj.root=this; | |
LIST.add(obj); | |
System.out.println("Direcktory "+ name +" has been added to "+this+"\n"); | |
return; | |
} | |
else | |
System.out.println("Direcktory "+ name +" is already added to "+this+"\n"); | |
} | |
protected void addobj(VirtyalFileSystem obj){ | |
VirtyalFileSystem o = find1(obj.toString()); | |
if (o==null){ | |
obj.root=this; | |
LIST.add(obj); | |
System.out.println(obj +" has been added to "+this+"\n"); | |
return; | |
} | |
else | |
System.out.println(o+" is already added to "+this); | |
} | |
protected void showAll(){ | |
for(VirtyalFileSystem fso : this.LIST){ | |
int distanse = 60 - (fso.getPath().length() + fso.getDate().length()); | |
StringBuffer sb = new StringBuffer(); | |
StringBuffer sb1 = new StringBuffer(); | |
sb1.setLength(distanse); | |
//sb.setLength(SavPoz + 5); | |
sb.append(fso.getPath()) | |
.append(sb1) | |
.append(fso.getDate()); | |
System.out.println(sb); | |
fso.showAll(); | |
} | |
} | |
public static void prnFile(int SavPoz, String File, String Date){ | |
StringBuffer sb = new StringBuffer(); | |
//sb.setLength(SavPoz + 5); | |
sb.append(File) | |
.append(" ") | |
.append(Date); | |
System.out.println(sb); | |
} | |
public String getDate() { | |
SimpleDateFormat stf=new SimpleDateFormat(); | |
return stf.format(d); | |
} | |
protected void toSerialize(File f) throws Exception{ | |
ObjectOutputStream obout = new ObjectOutputStream(new FileOutputStream(f)); | |
obout.writeObject(this); | |
System.out.println(this+" has been serialized to "+ f+"\n"); | |
obout.flush(); | |
obout.close(); | |
} | |
public String getName() { | |
return name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment