-----<a target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! ------
package com.itheima;
package com.filesystem.method;
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.Node;
import com.filesystem.beans.User;
public class MethodUtil {
private DiskContain diskContain = null;
private List<User> list_user = null;
public MethodUtil(DiskContain diskContain,List<User> list_user)
{
this.diskContain = diskContain;
this.list_user = list_user;
}
public char[] getDiskAllDiskContain() //从模拟硬盘得到要读写的空间
{
return diskContain.getDiskcontain();
}
public int getDiskSpare() //得到硬盘闲置空间大小
{
return this.getDiskAllLength()-getDiskOccupy();
}
public int getDiskAllLength() //得到硬盘空间的总大小
{
return this.getDiskAllDiskContain().length;
}
public int getDiskOccupy() //得到已使用的硬盘空间大小
{
char[] diskcon = this.getDiskAllDiskContain();
int position = 0;
for(int i=0;i<diskcon.length;i++)
{
if('@'==diskcon[i])
{
position=i;
break;
}
}
return position;
}
public boolean checkLogin(User user) //检验用户登录
{
for(int i = 0;i<list_user.size();i++)
{
if(list_user.get(i).getName().equals(user.getName())&&list_user.get(i).getPassword().equals(user.getPassword()))
{
return true;
}
}
return false;
}
public File findFile(String fileName,User user)//找到相应的文件
{
List<File> list_file = user.getList_file();
Iterator<File> it_file = list_file.iterator();
while(it_file.hasNext())
{
File file = it_file.next();
if(file.getFileName().equals(fileName))
{
return file;
}
}
return null;
}
public List<File> getListFileSub(String fileParentName,User user) //得到子目录下的文件
{
List<File> list_sub = new ArrayList<File>();
List<File> list_all = user.getList_file();
Iterator<File> it_all = list_all.iterator();
while(it_all.hasNext())
{
File file_son = it_all.next();
if(fileParentName.equals(file_son.getFileName_parent()))
{
list_sub.add(file_son);
}
}
return list_sub;
}
public void writeToFile(String fileName,String content,User user) //向文件中写入内容
{
File file = this.findFile(fileName, user);
if(file.getStatus()==0)
{
System.out.println("文件没有打开,请先打开文件!");
return;
}
if(this.getDiskSpare()<content.length())
{
System.out.println("存储空间不够,请插入其他存储设备!");
return;
}
char[] ch_con = content.toCharArray();
int startPosition = this.getDiskOccupy();
int length = ch_con.length;
int endPosition = startPosition+length;
char[] ch_disk = this.diskContain.getDiskcontain();
for(int i = 0;i<ch_con.length;i++)
{
ch_disk[i+startPosition] = ch_con[i];
}
file.getList_node().add(new Node(startPosition,length,endPosition));
System.out.println("内容写入文件成功");
}
public String readFile(String fileName,User user) //读文件相应的文件
{
File file = this.findFile(fileName, user);
if(file.getStatus()==0)
{
System.out.println("文件没有打开,请先打开文件!");
return null;
}
char[] ch_disk = this.diskContain.getDiskcontain();
StringBuilder str_builder = new StringBuilder();
List<Node> list_node = file.getList_node();
Iterator<Node> it_node = list_node.iterator();
while(it_node.hasNext())
{
Node node = it_node.next();
for(int i = node.getStartPosition();i<node.getEndPosition();i++)
{
str_builder.append(ch_disk[i]);
}
}
return str_builder.toString();
}
public void createFile(String fileName,String fileName_parent,User user)//创建文件
{
user.getList_file().add(new File(fileName,fileName_parent));
}
public void openFile(String fileName,User user)//打开文件
{
File file = this.findFile(fileName, user);
if(file.getStatus()==0)
{
file.setStatus(1);
System.out.println("文件打开成功");
return;
}else
{
System.out.println("文件已经被打开");
return;
}
}
public void closeFile(String fileName,User user)//关闭文件
{
File file = this.findFile(fileName, user);
if(file.getStatus()==1)
{
file.setStatus(0);
System.out.println("文件关闭成功");
return;
}else
{
System.out.println("文件已经被关闭");
return;
}
}
public void deleteFile(String fileName,User user)//删除文件
{
List<File> list_file = user.getList_file();
Iterator<File> it_file = list_file.iterator();
while(it_file.hasNext())
{
File file = it_file.next();
if(file.getFileName().equals(fileName))
{
list_file.remove(file);
System.out.println("删除文件成功");
return;
}
}
return;
}
}
|
|