📄 myfilesystem.java
字号:
{
file[n]=data[getNumber][n];
}
file[5]="open";
data[getNumber][5]="open";
System.out.println("file :"+cmd[1]+" 已经打开 ");
break;
}
}
else
{ if(i==99)
{
System.out.println("文件不存在");
break;
}
}
}
}//end of for()
}
else if(cmd[0].trim().equals("close"))
{
for(int n=0;n<7;n++)
{
data[getNumber][n]=file[n];
}
data[getNumber][5]="close";
file[5]="close";
System.out.println("文件"+file[0]+"已经关闭");
}
else if(cmd[0].trim().equals("read"))
{
for(int i=0;i<100;i++)
{ if(data[i][0].equals(cmd[1]))
{if(data[i][1].equals(username))
{
for(int n=0;n<7;n++)
{
file[n]=data[i][n];
}
if(file[5].equals("close"))
{
System.out.println("文件没有打开! ");
break;
}
else if(file[5].equals("open"))
{
System.out.println(file[6]);
break;
}
}
}
}
}
else if(cmd[0].trim().equals("write"))
{ String changeString="";
String chose="";
for(int i=0;i<100;i++)
{ if(data[i][0].equals(cmd[1]))
{if(data[i][1].equals(username))
{
for(int n=0;n<7;n++)
{
file[n]=data[i][n];
}
if(file[5].equals("close"))
{
System.out.println("文件没有打开! ");
break;
}
else if(file[5].equals("open"))
{
if(file[4].equals("1"))
{
System.out.println("只读文件,不能改写");
break;
}
else { System.out.println("输入修改数据");
changeString=input.readLine();
while(true)
{
System.out.println("修改选择:全部修改 1 增加数据 2");
chose=input.readLine();
if(chose.equals("1"))
{ file[6]=changeString;
file[3]=String.valueOf(file[6].length());
break;
}
else if(chose.equals("2"))
{ file[6]+=changeString;
break;
}
else continue;
}
System.out.println(" 修改后数据:");
System.out.println(file[6]);
file[3]=String.valueOf(file[6].length());
}
}
}
}
}
}
else if(cmd[0].trim().equals("exit"))
{
int f;
for(f=0;f<100;f++)
{
if(data[f][5].equals("open"))
{ System.out.println("有文件在内存中,请先关闭");
break;
}
}
if(f==100)
{
write("d:\\FileDisk.txt");
System.out.println("用户"+username+"退出系统。 谢谢支持!");
return;
}
}
else if(cmd[0].trim().equals("help"))
{ System.out.println();
System.out.print("create ");System.out.println("创建文件");
System.out.print("dir ");System.out.println("列目录文件");
System.out.print("exit ");System.out.println("退出");
System.out.println("以下命令需加文件名");
System.out.println("eg:open ***");
System.out.print("open ");System.out.println("打开文件");
System.out.print("close ");System.out.println("关闭文件");
System.out.print("read ");System.out.println("读文件");
System.out.print("write ");System.out.println("写文件");
System.out.print("delete ");System.out.println("删除文件");
System.out.println();
}
else System.out.println("错误命令");
}//end of while(true)
}//end of commondFS
//////////////////////////////////
public static void write(String filename)
{
try{
File file=new File(filename);
FileWriter writeOut=new FileWriter(file);
String writeString="";
for(int i=0;i<100;i++)
for(int j=0;j<7;j++)
{
writeString+=data[i][j]+"#";
}
writeOut.write(writeString);
writeOut.close();
} catch(IOException e){
String err=e.toString();
System.out.println(err);
System.out.println("保存文件错误....");}
}
///////////////////////////
public static void writename(String usenamefile)
{
try{
File file=new File(usenamefile);
FileWriter writeOut=new FileWriter(file);
String nameString="";
for(int j=0;j<10;j++)
{
nameString+=userName[j]+"#";
}
writeOut.write(nameString);
writeOut.close();
} catch(IOException e){
String err=e.toString();
System.out.println(err);
System.out.println("保存文件错误....");}
}
/////////////////////////////////////////
public static void readname(String usernamefile)
{
try{
File file=new File(usernamefile);
FileReader readIn=new FileReader(file);
int length=(int)file.length();
int readChars=0;
char[] content=new char[length];
while(readIn.ready())
readChars+=readIn.read(content,readChars,length-readChars);
readIn.close();
String readNF=new String(content,0,readChars);
StringTokenizer parser=new StringTokenizer(readNF,"#");
int n=0;
while(parser.hasMoreTokens())
{
userName[n]=parser.nextToken();
n++;
}
}
catch(IOException e){ String err=e.toString();
System.out.println(err);
System.out.println("读入文件错误....");}
}
/////////////////////////
public static void read(String filename)
{
try{
File file=new File(filename);
FileReader readIn=new FileReader(file);
int length=(int)file.length();
int readChars=0;
char[] content=new char[length];
while(readIn.ready())
readChars+=readIn.read(content,readChars,length-readChars);
readIn.close();
String readFD=new String(content,0,readChars);
StringTokenizer parser=new StringTokenizer(readFD,"#");
int n=0;
String temp[]=new String[900];
while(parser.hasMoreTokens())
{
temp[n]=parser.nextToken();
n++;
}
for(int i=0;i<100;i++)
for(int j=0;j<7;j++)
{ data[i][j]=temp[i*7+j];
}
}
catch(IOException e){ String err=e.toString();
System.out.println(err);
System.out.println("读入文件错误....");}
}
}//end of class MFS
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -