-----<a target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! ------
package com.itheima;
package com.filesystem.Main;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.filesystem.beans.DiskContain;
import com.filesystem.beans.File;
import com.filesystem.beans.User;
import com.filesystem.method.FromScreen;
import com.filesystem.method.MethodUtil;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FromScreen fs = new FromScreen();
DiskContain diskContain = new DiskContain();
List<User> list_user = new ArrayList<User>();
list_user.add(new User("root","computer"));
MethodUtil mu = new MethodUtil(diskContain,list_user);
System.out.println("dir 列目录");
System.out.println("create 创建文件");
System.out.println("delete 删除文件");
System.out.println("open 打开文件");
System.out.println("close 关闭文件");
System.out.println("read 读文件");
System.out.println("write 写文件");
System.out.println("mkdir 创建目录");
System.out.println("rmdir 删除目录");
System.out.println("cd 跳转目录");
User user = new User();
List<String> list_str = new ArrayList<String>();
list_str.add("");
while(true)
{
user = fs.getUserFromScreen();
if(mu.checkLogin(user))
{
System.out.println("用户登录成功");
System.out.print("$"+user.getName()+">");
break;
}
}
while(true)
{
String str_screen = fs.getStringFromScreen();
if(str_screen.startsWith("dir"))
{
String str_parent = list_str.get(list_str.size()-1);
List<File> list_file = mu.getListFileSub(str_parent, user);
Iterator<File> it_file = list_file.iterator();
while(it_file.hasNext())
{
File file = it_file.next();
System.out.println("fileName:"+file.getFileName());
}
System.out.print("$"+user.getName()+">");
continue;
}else if(str_screen.startsWith("create"))
{
String str_parent = list_str.get(list_str.size()-1);
String fileName = str_screen.substring(str_screen.indexOf("create ")+7);
mu.createFile(fileName, str_parent, user);
System.out.println("创建文件成功");
System.out.print("$"+user.getName()+">");
continue;
}else if(str_screen.startsWith("delete"))
{
String fileName = str_screen.substring(str_screen.indexOf("delete ")+7);
mu.deleteFile(fileName, user);
//System.out.println("删除文件成功");
System.out.print("$"+user.getName()+">");
continue;
}else if(str_screen.startsWith("open"))
{
String fileName = str_screen.substring(str_screen.indexOf("open ")+5);
mu.openFile(fileName, user);
System.out.print("$"+user.getName()+">");
continue;
}else if(str_screen.startsWith("close"))
{
String fileName = str_screen.substring(str_screen.indexOf("close ")+6);
mu.closeFile(fileName, user);
System.out.print("$"+user.getName()+">");
continue;
}else if(str_screen.startsWith("read"))
{
String fileName = str_screen.substring(str_screen.indexOf("read ")+5);
String str_cont = mu.readFile(fileName, user);
System.out.println(str_cont);
System.out.print("$"+user.getName()+">");
continue;
}else if(str_screen.startsWith("write"))
{
String fileName = str_screen.substring(str_screen.indexOf(" ")+1, str_screen.lastIndexOf(" "));
String content = str_screen.substring(str_screen.lastIndexOf(" ")+1);
mu.writeToFile(fileName, content, user);
System.out.print("$"+user.getName()+">");
continue;
}else if(str_screen.startsWith("mkdir"))
{
String str_dir = str_screen.substring(str_screen.indexOf("mkdir ")+6);
String[] str_arr = str_dir.split("/");
for(int i=1;i<str_arr.length;i++)
{
list_str.add(str_arr[i]);
mu.createFile(str_arr[i], list_str.get(list_str.size()-2), user);
}
System.out.println("文件目录创建成功");
System.out.print("$"+user.getName()+">");
continue;
}else if(str_screen.startsWith("cd "))
{
String str_dir = str_screen.substring(str_screen.indexOf("cd ")+3);
if(list_str.contains(str_dir))
{
if((list_str.indexOf(str_dir)+1)<=list_str.size())
{
int index = list_str.indexOf(str_dir);
int length = list_str.size();
for(int i = index+1;i<length;i++)
{
list_str.remove(i);
}
}
}else
{
list_str.add(str_dir);
}
System.out.println("文件目录跳转成功");
System.out.print("$"+user.getName()+">");
continue;
}else if(str_screen.startsWith("cd.."))
{
list_str.remove(list_str.size()-1);
System.out.println("返回上级目录成功");
System.out.print("$"+user.getName()+">");
continue;
}else if(str_screen.startsWith("cd/"))
{
list_str.clear();
list_str.add("");
System.out.println("返回根目录成功");
System.out.print("$"+user.getName()+">");
continue;
}else if(str_screen.startsWith("rmdir"))
{
String str_dir = str_screen.substring(str_screen.indexOf("rmdir ")+6);
if(list_str.contains(str_dir))
{
if((list_str.indexOf(str_dir)+1)<=list_str.size())
{
int index = list_str.indexOf(str_dir);
int length = list_str.size();
for(int i = index+1;i<length;i++)
{
list_str.remove(i);
}
}
}
List<File> list_f = user.getList_file();
Iterator<File> it_f = list_f.iterator();
while(it_f.hasNext())
{
File f = it_f.next();
if(f.getFileName_parent().equals(str_dir))
{
list_f.remove(f);
}
}
System.out.println("文件目录删除成功");
System.out.print("$"+user.getName()+">");
continue;
}
}
}
}
|
|