几乎所有常用文件操作的示例代码

首先我们来看代码:
//几乎所有常用文件操作的示例代码
import java.io.*;
public class TestIO{
public static void main(String[] args)
throws IOException{
//1.以行为单位从一个文件读取数据
BufferedReader in =
new BufferedReader(
new FileReader("F:\\nepalon\\TestIO.java"));
String s, s2 = new String();
while((s = in.readLine()) != null)
s2 += s + "\n";
in.close();
//1b. 接收键盘的输入
BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter a line:");
System.out.println(stdin.readLine());
//2. 从一个String对象中读取数据
StringReader in2 = new StringReader(s2);
int c;
while((c = in2.read()) != -1)
System.out.println((char)c);
in2.close();
//3. 从内存取出格式化输入
try{
DataInputStream in3 =
new DataInputStream(
new ByteArrayInputStream(s2.getBytes()));
while(true)
System.out.println((char)in3.readByte());
}
catch(EOFException e){
System.out.println("End of stream");
}
//4. 输出到文件
try{
BufferedReader in4 =
new BufferedReader(
new StringReader(s2));
PrintWriter out1 =
new PrintWriter(
new BufferedWriter(
new FileWriter("F:\\nepalon\\ TestIO.out")));
int lineCount = 1;
while((s = in4.readLine()) != null)
out1.println(lineCount++ + ":" + s);
out1.close();
in4.close();
}
catch(EOFException ex){
System.out.println("End of stream");
}
//5. 数据的存储和恢复
try{
DataOutputStream out2 =
new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream("F:\\nepalon\\ Data.txt")));
out2.writeDouble(3.1415926);
out2.writeChars("\nThas was pi:writeChars\n");
out2.writeBytes("Thas was pi:writeByte\n");
out2.close();
DataInputStream in5 =
new DataInputStream(
new BufferedInputStream(
new FileInputStream("F:\\nepalon\\ Data.txt")));
BufferedReader in5br =
new BufferedReader(
new InputStreamReader(in5));
System.out.println(in5.readDouble());
System.out.println(in5br.readLine());
System.out.println(in5br.readLine());
}
catch(EOFException e){
System.out.println("End of stream");
}
//6. 通过RandomAccessFile操作文件
RandomAccessFile rf =
new RandomAccessFile("F:\\nepalon\\ rtest.dat", "rw");
for(int i=0; i<10; i++)
rf.writeDouble(i*1.414);
rf.close();
rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "r");
for(int i=0; i<10; i++)
System.out.println("Value " + i + ":" + rf.readDouble());
rf.close();
rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "rw");
rf.seek(5*8);
rf.writeDouble(47.0001);
rf.close();
rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "r");
for(int i=0; i<10; i++)
System.out.println("Value " + i + ":" + rf.readDouble());
rf.close();
}
}
关于代码的解释(以区为单位):
1区中,当读取文件时,先把文件内容读到缓存中,当调用in.readLine()时,再从缓存中以字符的方式读取数据(以下简称"缓存字节读取方式")。
1b区中,由于想以缓存字节读取方式从标准IO(键盘)中读取数据,所以要先把标准IO(System.in)转换成字符导向的stream,再进行BufferedReader封装。
2区中,要以字符的形式从一个String对象中读取数据,所以要产生一个StringReader类型的stream。
4区中,对String对象s2读取数据时,先把对象中的数据存入缓存中,再从缓冲中进行读取;对TestIO.out文件进行操作时,先把格式化后的信息输出到缓存中,再把缓存中的信息输出到文件中。
5 区中,对Data.txt文件进行输出时,是先把基本类型的数据输出屋缓存中,再把缓存中的数据输出到文件中;对文件进行读取操作时,先把文件中的数据读取到缓存中,再从缓存中以基本类型的形式进行读取。注意in5.readDouble()这一行。因为写入第一个writeDouble(),所以为了正确显示。也要以基本类型的形式进行读取。
6区是通过RandomAccessFile类对文件进行操作。
讲解部分:
Input和Output
1. stream代表的是任何有能力产出数据的数据源,或是任何有能力接收数据的接收源。在Java的IO中,所有的stream(包括Input和Out stream)都包括两种类型:
1.1 以字节为导向的stream
以字节为导向的stream,表示以字节为单位从stream中读取或往stream中写入信息。以字节为导向的stream包括下面几种类型:
1) input stream:
1) ByteArrayInputStream:把内存中的一个缓冲区作为InputStream使用
2) StringBufferInputStream:把一个String对象作为InputStream
3) FileInputStream:把一个文件作为InputStream,实现对文件的读取操作
4) PipedInputStream:实现了pipe的概念,主要在线程中使用
5) SequenceInputStream:把多个InputStream合并为一个InputStream
2) Out stream
1) ByteArrayOutputStream:把信息存入内存中的一个缓冲区中
2) FileOutputStream:把信息存入文件中
3) PipedOutputStream:实现了pipe的概念,主要在线程中使用
4) SequenceOutputStream:把多个OutStream合并为一个OutStream
1.2 以Unicode字符为导向的stream
以Unicode字符为导向的stream,表示以Unicode字符为单位从stream中读取或往stream中写入信息。以Unicode字符为导向的stream包括下面几种类型:
1) Input Stream
1) CharArrayReader:与ByteArrayInputStream对应
2) StringReader:与StringBufferInputStream对应
3) FileReader:与FileInputStream对应
4) PipedReader:与PipedInputStream对应
2) Out Stream
1) CharArrayWrite:与ByteArrayOutputStream对应
2) StringWrite:无与之对应的以字节为导向的stream
3) FileWrite:与FileOutputStream对应
4) PipedWrite:与PipedOutputStream对应
以字符为导向的stream基本上对有与之相对应的以字节为导向的stream。两个对应类实现的功能相同,字是在操作时的导向不同。如 CharArrayReader:和ByteArrayInputStream的作用都是把内存中的一个缓冲区作为InputStream使用,所不同的是前者每次从内存中读取一个字节的信息,而后者每次从内存中读取一个字符。
1.3 两种不现导向的stream之间的转换
InputStreamReader和OutputStreamReader:把一个以字节为导向的stream转换成一个以字符为导向的stream。
2. stream添加属性
2.1 "为stream添加属性"的作用
运用上面介绍的Java中操作IO的API,我们就可完成我们想完成的任何操作了。但通过FilterInputStream和FilterOutStream的子类,我们可以为stream添加属性。下面以一个例子来说明这种功能的作用。
如果我们要往一个文件中写入数据,我们可以这样操作:
FileOutStream fs = new FileOutStream("test.txt");
然后就可以通过产生的fs对象调用write()函数来往test.txt文件中写入数据了。但是,如果我们想实现"先把要写入文件的数据先缓存到内存中,再把缓存中的数据写入文件中"的功能时,上面的API就没有一个能满足我们的需求了。但是通过FilterInputStream和 FilterOutStream的子类,为FileOutStream添加我们所需要的功能。
2.2 FilterInputStream的各种类型
2.2.1 用于封装以字节为导向的InputStream
1) DataInputStream:从stream中读取基本类型(int、char等)数据。
2) BufferedInputStream:使用缓冲区
3) LineNumberInputStream:会记录input stream内的行数,然后可以调用getLineNumber()和setLineNumber(int)
4) PushbackInputStream:很少用到,一般用于编译器开发
2.2.2 用于封装以字符为导向的InputStream
1) 没有与DataInputStream对应的类。除非在要使用readLine()时改用BufferedReader,否则使用DataInputStream
2) BufferedReader:与BufferedInputStream对应
3) LineNumberReader:与LineNumberInputStream对应
4) PushBackReader:与PushbackInputStream对应
2.3 FilterOutStream的各种类型
2.2.3 用于封装以字节为导向的OutputStream
1) DataIOutStream:往stream中输出基本类型(int、char等)数据。
2) BufferedOutStream:使用缓冲区
3) PrintStream:产生格式化输出
2.2.4 用于封装以字符为导向的OutputStream
1) BufferedWrite:与对应
2) PrintWrite:与对应
3. RandomAccessFile
1) 可通过RandomAccessFile对象完成对文件的读写操作
2) 在产生一个对象时,可指明要打开的文件的性质:r,只读;w,只写;rw可读写
3) 可以直接跳到文件中指定的位置
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <stdio.h> #include <memory.h> #include <string> #include <iostream> using namespace std; //1代表普通文件2代表目录文件0表示空文件 #define GENERAL 1 #define DIRECTORY 2 #define HXSH 0 struct FCB { char fname[16]; //文件名 char type; int size; //文件大小 int fatherBlockNum; //当前的父目录盘块号 int currentBlockNum; //当前的盘块 void initialize() { strcpy(fname,"\0"); type = HXSH; size =0; fatherBlockNum = currentBlockNum = 0; } }; /*常量设置*/ const char* FilePath = "C:\\myfiles"; const int BlockSize = 512; //盘块大小 const int OPEN_MAX = 5; //能打开最多的文件数 const int BlockCount = 128; //盘块数 const int DiskSize = BlockSize*BlockCount; //磁盘大小 const int BlockFcbCount = BlockSize/sizeof(FCB);//目录文件的最多FCB数 int OpenFileCount = 0; struct OPENLIST //用户文件打开表 { int files; //当前打开文件数 FCB f[OPEN_MAX]; //FCB拷贝 OPENLIST() { files=0; for(int i=0;i<OPEN_MAX;i++){ f[i].fatherBlockNum=-1;//为分配打开 f[i].type=GENERAL; } } }; /*-------------目录文件结构---------------*/ struct dirFile { struct FCB fcb[BlockFcbCount]; void init(int _FatherBlockNum,int _CurrentBlockNum,char *name)//父块号,当前块号,目录名 { strcpy(fcb[0].fname,name); //本身的FCB fcb[0].fatherBlockNum=_FatherBlockNum; fcb[0].currentBlockNum=_CurrentBlockNum; fcb[0].type=DIRECTORY; //标记目录文件 for(int i=1;i<BlockFcbCount;i++){ fcb[i].fatherBlockNum=_CurrentBlockNum; //标记为子项 fcb[i].type=HXSH; // 标记为空白项 } } }; /**********************************************************************/ struct DISK { int FAT1[BlockCount]; //FAT1 int FAT2[BlockCount]; //FAT2 struct dirFile root; //根目录 char data[BlockCount-3][BlockSize]; void format() { memset(FAT1,0,BlockCount); //FAT1 memset(FAT2,0,BlockCount); //FAT2 FAT1[0]=FAT1[1]=FAT1[2]=-2; //0,1,2盘块号依次代表FAT1,FAT2,根目录区 FAT2[0]=FAT2[1]=FAT2[2]=-2; //FAT作备份 root.init(2,2,"C:\\");//根目录区 memset(data,0,sizeof(data));//数据区 } }; /*-----------------全局变量--------------------------*/ FILE *fp; //磁盘文件地址 char * BaseAddr; //虚拟磁盘空间基地址 string currentPath="C:\\\\"; //当前路径 int current=2; //当前目录的盘块号 string cmd; //输入指令 struct DISK *osPoint; //磁盘操作系统指针 char command[16]; //文件名标识 struct OPENLIST* openlist; //用户文件列表指针 /*-----------函数事先申明--------------------*/ int format(); int mkdir(char *sonfname); int rmdir(char *sonfname); int create(char *name); int listshow(); int delfile(char *name); int changePath(char *sonfname); int write(char *name); int exit(); int open(char *file); int close(char *file); int read(char *file); /*------------初始化-----------------------*/ int format() { current = 2; currentPath="C:\\\\"; //当前路径 osPoint->format();//打开文件列表初始化 delete openlist; openlist=new OPENLIST; /*-------保存到磁盘上myfiles--------*/ fp = fopen(FilePath,"w+"); fwrite(BaseAddr,sizeof(char),DiskSize,fp); fclose(fp); printf("----------------------------------------------------------\n\n"); return 1; } /*-----------------------创建子目录-------------------*/ int mkdir(char *sonfname) { //判断是否有重名 //寻找空白子目录项 //寻找空白盘块号 //当前目录下增加该子目录项 //分配子目录盘块,并且初始化 //修改fat表 int i,temp,iFAT; struct dirFile *dir; //当前目录的指针 if(current==2) dir=&(osPoint->root); else dir=(struct dirFile *)(osPoint->data [current-3]); /*--------为了避免该目录下同名文件夹--------*/ for(i = 1;i<BlockFcbCount;i++) { if(dir->fcb[i].type==DIRECTORY && strcmp(dir->fcb[i].fname,sonfname)==0 ) { printf("该文件夹下已经有同名的文件夹存在了!\n"); return 0; } } //查找空白fcb序号 for(i=1;i<BlockFcbCount;i++) { if(dir->fcb[i].type==HXSH) break; } if(i==BlockFcbCount) { printf("该目录已满!请选择新的目录下创建!\n"); return 0; } temp=i; for(i = 3;i < BlockCount;i++) { if(osPoint->FAT1[i] == 0) break; } if(i == BlockCount) { printf("磁盘已满!\n"); return 0; } iFAT=i; /*-------------接下来进行分配----------*/ osPoint->FAT1[iFAT]=osPoint->FAT2[iFAT] = 2; //2表示分配给下级目录文件 //填写该分派新的盘块的参数 strcpy(dir->fcb[temp].fname,sonfname); dir->fcb[temp].type=DIRECTORY; dir->fcb[temp].fatherBlockNum=current; dir->fcb[temp].currentBlockNum=iFAT; //初始化子目录文件盘块 dir=(struct dirFile*)(osPoint->data [iFAT-3]); //定位到子目录盘块号 dir->init (current,iFAT,sonfname);//iFAT是要分配的块号,这里的current用来指要分配的块的父块号 printf("---------------------------------------------------------------\n\n"); return 1; } /*-------删除当前目录下的文件夹--------*/ int rmdir(char *sonfname) { int i,temp,j;//确保当前目录下有该文件,并记录下该FCB下标 struct dirFile *dir; //当前目录的指针 if(current==2) dir=&(osPoint->root); else dir=(struct dirFile *)(osPoint->data [current-3]); for(i=1;i<BlockFcbCount;i++) { //查找该目录文件 if(dir->fcb[i].type==DIRECTORY && strcmp(dir->fcb[i].fname,sonfname)==0) { break; } } temp=i; if(i==BlockFcbCount) { printf("当前目录下不存在该子目录!\n"); return 0; } j = dir->fcb[temp].currentBlockNum; struct dirFile *sonDir; //当前子目录的指针 sonDir=(struct dirFile *)(osPoint->data [ j - 3]); for(i=1;i<BlockFcbCount;i++) //查找子目录是否为空目录 { if(sonDir->fcb[i].type!=HXSH) { printf("该文件夹为非空文件夹,为确保安全,请清空后再删除!\n"); return 0; } } /*开始删除子目录操作*/ osPoint->FAT1[j] = osPoint->FAT2[j]=0; //fat清空 char *p=osPoint->data[j-3]; //格式化子目录 memset(p,0,BlockSize); dir->fcb[temp].initialize(); //回收子目录占据目录项 printf("---------------------------------------------------------------\n\n"); return 1; } /*-----------在当前目录下创建文本文件---------------*/ int create(char *name) { int i,iFAT;//temp, int emptyNum = 0,isFound = 0; //空闲目录项个数 struct dirFile *dir; //当前目录的指针 if(current==2) dir=&(osPoint->root); else dir=(struct dirFile *)(osPoint->data [current-3]); //查看目录是否已满 //为了避免同名的文本文件 for(i=1;i<BlockFcbCount;i++) { if(dir->fcb[i].type == HXSH && isFound == 0) { emptyNum = i; isFound = 1; } else if(dir->fcb[i].type==GENERAL && strcmp(dir->fcb[i].fname,name)==0 ) { printf("无法在同一目录下创建同名文件!\n"); return 0; } } if(emptyNum == 0) { printf("已经达到目录项容纳上限,无法创建新目录!\n"); return 0; } //查找FAT表寻找空白区,用来分配磁盘块号j for(i = 3;i<BlockCount;i++) { if(osPoint->FAT1[i]==0) break; } if(i==BlockCount) { printf("磁盘已满!\n"); return 0; } iFAT=i; /*------进入分配阶段---------*/ //分配磁盘块 osPoint->FAT1[iFAT] = osPoint->FAT2[iFAT] = 1; /*-----------接下来进行分配----------*/ //填写该分派新的盘块的参数 strcpy(dir->fcb[emptyNum].fname,name); dir->fcb[emptyNum].type=GENERAL; dir->fcb[emptyNum].fatherBlockNum=current; dir->fcb[emptyNum].currentBlockNum=iFAT; dir->fcb[emptyNum].size =0; char* p = osPoint->data[iFAT -3]; memset(p,4,BlockSize); printf("----------------------------------------------------------------\n\n"); return 1; } /*-------查询子目录------------*/ int listshow() { int i,DirCount=0,FileCount=0; //搜索当前目录 struct dirFile *dir; //当前目录的指针 if(current==2) dir=&(osPoint->root); else dir=(struct dirFile *)(osPoint->data [current-3]); for(i=1;i<BlockFcbCount;i++) { if(dir->fcb[i].type==GENERAL) { //查找普通文件 FileCount++; printf("%s 文本文件.\n",dir->fcb[i].fname); } if(dir->fcb[i].type==DIRECTORY) { //查找目录文件 DirCount++; printf("%s 文件夹.\n",dir->fcb[i].fname); } } printf("\n该目录下共有 %d 个文本文件, %d 个文件夹\n\n",FileCount,DirCount); printf("--------------------------------------------------------\n\n"); return 1; } /*---------在当前目录下删除文件-----------*/ int delfile(char *name) { int i,temp,j; //确保当前目录下有该文件,并且记录下它的FCB下标 struct dirFile *dir; //当前目录的指针 if(current==2) dir=&(osPoint->root); else dir=(struct dirFile *)(osPoint->data [current-3]); for(i=1;i<BlockFcbCount;i++) //查找该文件 { if(dir->fcb[i].type==GENERAL && strcmp(dir->fcb[i].fname,name)==0) { break; } } if(i==BlockFcbCount) { printf("当前目录下不存在该文件!\n"); return 0; } int k; for(k=0;k<OPEN_MAX;k++) { if((openlist->f [k].type = GENERAL)&& (strcmp(openlist->f [k].fname,name)==0)) { if(openlist->f[k].fatherBlockNum == current) { break; } else { printf("该文件未在当前目录下!\n"); return 0; } } } if(k!=OPEN_MAX) { close(name); } //从打开列表中删除 temp=i; /*开始删除文件操作*/ j = dir->fcb [temp].currentBlockNum ; //查找盘块号j osPoint->FAT1[j]=osPoint->FAT2[j]=0; //fat1,fat2表标记为空白 char *p=osPoint->data[j - 3]; memset(p,0,BlockSize); //清除原文本文件的内容 dir->fcb[temp].initialize(); //type=0; //标记该目录项为空文件 printf("------------------------------------------------------------\n\n"); return 1; } /*--------------进入当前目录下的子目录--------------*/ int changePath(char *sonfname) { struct dirFile *dir; //当前目录的指针 if(current==2) dir=&(osPoint->root); else dir=(struct dirFile *)(osPoint->data [current-3]); /*回到父目录*/ if(strcmp(sonfname,"..")==0) { if(current==2) { printf("你现已经在根目录下!\n"); return 0; } current = dir->fcb[0].fatherBlockNum ; currentPath = currentPath.substr(0,currentPath.size() - strlen(dir->fcb[0].fname )-1); return 1; } /*进入子目录*/ int i,temp; //确保当前目录下有该目录,并且记录下它的FCB下标 for(i = 1; i < BlockFcbCount; i++) { //查找该文件 if(dir->fcb[i].type==DIRECTORY && strcmp(dir->fcb[i].fname,sonfname)==0 ) { temp=i; break; } } if(i==BlockFcbCount) { printf("不存在该目录!\n"); return 0; } //修改当前文件信息 current=dir->fcb [temp].currentBlockNum ; currentPath = currentPath+dir->fcb [temp].fname +"\\"; printf("-------------------------------------------------------------\n\n"); return 1; } /*--------System exit---------------------*/ int exit() { //将所有文件都关闭 //保存到磁盘上C:\myfiles fp=fopen(FilePath,"w+"); fwrite(BaseAddr,sizeof(char),DiskSize,fp); fclose(fp); //释放内存上的虚拟磁盘 free(osPoint); //释放用户打开文件表 delete openlist; printf("---------------------------------------------------------\n\n"); return 1; } /*-------------在指定的文件里记录信息---------------*/ int write(char *name) { int i; char *startPoint,*endPoint; //在打开文件列表中查找 file(还需要考虑同名不同目录文件的情况!!!) for(i=0;i<OPEN_MAX;i++) { if(strcmp(openlist->f [i].fname,name)==0 ) { if(openlist->f[i].fatherBlockNum ==current) { break; } else { printf("该文件处于打开列表中,本系统只能改写当前目录下文件!\n"); return 0; } } } if(i==OPEN_MAX) { printf("该文件尚未打开,请先打开后写入信息!!\n"); return 0; } int active=i; int fileStartNum = openlist->f[active].currentBlockNum - 3 ; startPoint = osPoint->data[fileStartNum]; endPoint = osPoint->data[fileStartNum + 1]; printf("请输入文本以Ctrl D号结束:\t"); char input; while(((input=getchar())!=4)) { if(startPoint < endPoint-1) { *startPoint++ = input; } else { printf("达到单体文件最大容量!"); *startPoint++ = 4; break; } } return 1; } /*---------选择一个打开的文件读取信息----------*/ int read(char *file) { int i,fileStartNum; char *startPoint,*endPoint; //struct dirFile *dir; //在打开文件列表中查找 file(还需要考虑同名不同目录文件的情况!!!) for(i=0;i<OPEN_MAX;i++) { if(strcmp(openlist->f [i].fname,file)==0 ) { if(openlist->f[i].fatherBlockNum ==current) { break; } else { printf("该文件处于打开列表中,本系统只能阅读当前目录下文件!\n"); return 0; } } } if(i==OPEN_MAX) { printf("该文件尚未打开,请先打开后读取信息!\n"); return 0; } int active=i; //计算文件物理地址 fileStartNum = openlist->f[active].currentBlockNum - 3 ; startPoint = osPoint->data[fileStartNum]; endPoint = osPoint->data[fileStartNum + 1]; //end_dir=(struct dirFile *)[BlockSize-1]; //q=(char *)end_dir; printf("该文件的内容为: "); while((*startPoint)!=4&& (startPoint < endPoint)) { putchar(*startPoint++); } printf("\n"); return 1; } /*当前目录下添加一个打开文件*/ int open(char *file)//打开文件 { int i,FcbIndex; //确保没有打开过该文件 = 相同名字 + 相同目录 for(i=0;i<OPEN_MAX;i++) { if(openlist->f[i].type ==GENERAL && strcmp(openlist->f [i].fname,file)==0 &&openlist;->f[i].fatherBlockNum == current) { printf("该文件已经被打开!\n"); return 0; } } //确保有空的打开文件项 if(openlist->files == OPEN_MAX) { printf("打开文件数目达到上限!无法再打开新文件.\n"); return 0; } //确保当前目录下有该文件,并且记录下它的FCB下标 struct dirFile *dir; //当前目录的指针 if(current==2) dir=&(osPoint->root); else dir=(struct dirFile *)(osPoint->data [current-3]); for(i = 1;i< BlockFcbCount;i++) { //查找该文件 if(dir->fcb[i].type==GENERAL && strcmp(dir->fcb[i].fname,file)==0 ) { FcbIndex=i; break; } } if(i==BlockFcbCount) { printf("当前目录下不存在该文件!\n"); return 0; } //装载新文件进入打开文件列表,(FCB信息,文件数++) ??难道名字过不来? openlist->f[OpenFileCount] = dir->fcb[FcbIndex]; //FCB拷贝 openlist->files ++; printf("文件打开成功!\n"); OpenFileCount++; return 1; } int close(char *file) { //释放该文件所占内存 //释放用户打开文件列表表项 int i; //在打开文件列表中查找 file(还需要考虑同名不同目录文件的情况!!!) for(i=0;i<OPEN_MAX;i++) { if((openlist->f [i].type = GENERAL)&& (strcmp(openlist->f [i].fname,file)==0)) { if(openlist->f[i].fatherBlockNum == current) { break; } else { printf("该文件已打开,但未在当前目录下,无法关闭!\n"); return 0; } } } if(i==OPEN_MAX) { printf("该文件未在打开列表中!\n"); return 0; } int active=i; openlist->files --; openlist->f[active].initialize(); OpenFileCount--; printf("该文件已关闭!\n"); return 1; } int main() { /*********************************************************************/ printf("-----Welcome To My Operate System Of File(FAT)-----\n"); //使用说明书 printf("\n 以下是使用说明书:\n"); printf("--------------------------------------------------------------\n"); printf("|| format :对磁盘格式化. || \n"); printf("|| exit :安全退出该文件系统,保存信息. || \n"); printf("|| mkdir dirname :创建子目录. || \n"); printf("|| rmdir dirname :删除子目录. || \n"); printf("|| ls dirname :显示当前目录下信息. || \n"); printf("|| cd dirname :更改当前目录. || \n"); printf("|| create filename :创建一个文件,并且打开. || \n"); printf("|| write filename :选择一个打开的文件写入信息 || \n"); printf("|| read filename :选择一个打开的文件读取信息. || \n"); printf("|| rm filename :删除文件. || \n"); printf("|| open filename :打开文件. || \n"); printf("|| close filename :关闭文件. || \n"); printf("-------------------------------------------------------------\n\n"); //创建用户文件打开表 openlist=new OPENLIST; //申请虚拟空间并且初始化 BaseAddr=(char *)malloc(DiskSize); //虚拟磁盘初始化 osPoint=(struct DISK *)(BaseAddr); //加载磁盘文件 if((fp=fopen(FilePath,"r"))!=NULL){ fread(BaseAddr,sizeof(char),DiskSize,fp); printf("加载磁盘文件( %s )成功,现在可以进行操作了!\n\n",FilePath); } else{ printf("这是你第一次使用该文件管理系统!\t正在初始化...\n"); format(); printf("初始化已经完成,现在可以进行操作了!\n\n"); } printf("--------------------------------------------------------------\n\n"); while(1){ cout<<currentPath; cin>>cmd; if(cmd=="format"){ format(); } else if(cmd=="mkdir"){ cin>>command; mkdir(command); } else if(cmd=="rmdir"){ cin>>command; rmdir(command); } else if(cmd=="ls"){ listshow(); } else if(cmd=="cd"){ cin>>command; changePath(command); } else if(cmd=="create"){ cin>>command; create(command); } else if(cmd=="write"){ cin>>command; write(command); } else if(cmd=="read"){ cin>>command; read(command); } else if(cmd=="rm"){ cin>>command; delfile(command); } else if(cmd=="open"){ cin>>command; open(command); } else if(cmd=="close"){ cin>>command; close(command); } else if(cmd=="exit"){ exit(); break; } else cout<<"无效指令,请重新输入:"<<endl; } printf("Thank you for using my file system!\n"); return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值