import java.io.*;
import javax.swing.JOptionPane;
// 用户类 默认生成的用户名为 user1 user2 user3.....user10
class MDF{
String user;
MDF(){
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}
public class File_os {
static MDF []mdf=new MDF[11];
static String cmd;
static int searchUser(String name)// 查找是否存在当前用户
{
int i=1;
for(;i<11;i++){
if(mdf[i].getUser().equals(name)){
break;
}
}
return i;
}
public static void main(String args[]) throws IOException{
for(int i=1;i<=10;i++)
{
mdf[i]=new MDF();
}
for(int i=1;i<=10;i++){
mdf[i].setUser("user"+i);
}
String user=JOptionPane.showInputDialog("请输入用户名:");//输入用户名
String com;
int x=searchUser(user.toString().trim());
if(x==11)// 不存在用户
javax.swing.JOptionPane.showMessageDialog(null,"用户不存在!");
else// 存在用户
//System.out.println("存在此用户,请输入操作命令:");
cmd=JOptionPane.showInputDialog("存在此用户,请输入操作命令:\n -creat -delete -open -read -write -bye");
while(!cmd.equals("-bye")){
// 建立命令
if(cmd.equals("-输入的命令为:creat")){
System.out.println("creat");
String filename=JOptionPane.showInputDialog("请输入文件名:");
File file=new File("e:\\file\\",filename);
try{
file.createNewFile();
}catch(IOException e){
e.printStackTrace();
}
}
// 删除命令
else if(cmd.equals("-delete")){
System.out.println("输入的命令为:delete");
//输出该目录下的所有文件名
File file_names = new File("e:\\file\\");
File[] array = file_names.listFiles();
System.out.println("文件夹中存在的文件为:");
System.out.println("------------------");
for(int i=0;i<array.length;i++){
if(array[i].isFile()){
// only take file name
System.out.println("###"+array[i].getName()+"###");
}
}
String filename=JOptionPane.showInputDialog("请输入文件名:");
File file=new File("e:\\file\\",filename);
if(!file.exists()){
javax.swing.JOptionPane.showMessageDialog(null,"文件不存在!"); //如果输入文件不存在,则提出警告
}
file.delete();
javax.swing.JOptionPane.showMessageDialog(null,"删除成功!");
System.out.println("当前文件夹中存在的文件为:");
System.out.println("---------------------");
for(int i=0;i<array.length;i++){
if(array[i].isFile()){
System.out.println("###"+array[i].getName()+"###");
}
}
}
//打开命令
else if(cmd.equals("-open")){
System.out.println("输入的命令为:open");
//输出文件列表
File file_names = new File("e:\\file\\");
File[] array = file_names.listFiles();
System.out.println("文件夹中存在的文件为:");
System.out.println("------------------");
for(int i=0;i<array.length;i++){
if(array[i].isFile()){
// only take file name
System.out.println("###"+array[i].getName()+"###");
}
}
//判断文件是否存在
String filename=JOptionPane.showInputDialog("请输入文件名:");
File file=new File("e:\\file\\",filename);
if(!file.exists()){
javax.swing.JOptionPane.showMessageDialog(null,"文件不存在!"); //如果输入文件不存在,则提出警告
}
Runtime run = Runtime.getRuntime();
String f="e:\\file\\"+filename;
Process process=run.exec("notepad "+f);//打开一个文件
cmd=JOptionPane.showInputDialog("文件已经打开,如想关闭请输入操作命令: -close");
//关闭命令
if(cmd.equals("-close")){
System.out.println("文件已经关闭,输入的命令为:close");
process.destroy();
}
}
//读命令
else if(cmd.equals("-read")){
System.out.println("输入的命令为:read");
// 输出文件列表
File file_names = new File("e:\\file\\");
File[] array = file_names.listFiles();
System.out.println("文件夹中存在的文件为:");
System.out.println("------------------");
for(int i=0;i<array.length;i++){
if(array[i].isFile()){
// only take file name
System.out.println("###"+array[i].getName()+"###");
}
}
//输入要读的文件名
String filename=JOptionPane.showInputDialog("请输入文件名:");
File file=new File("e:\\file\\",filename);
if(!file.exists()){
javax.swing.JOptionPane.showMessageDialog(null,"文件不存在!"); //如果输入文件不存在,则提出警告
}
try{
FileReader fr=new FileReader(file);
char []data=new char[512];
int rs=0;
System.out.println("文件的内容为:");
while((rs=fr.read(data))>0){
String str=new String(data,0,rs);
System.out.println(str);
}
fr.close();
}catch(Exception e){
e.printStackTrace();
}
}
//写命令
else if(cmd.equals("-write")){
System.out.println("输入的命令为:write");
int b;
byte bytes[]=new byte[512];
// 输出文件列表
File file_names = new File("e:\\file\\");
File[] array = file_names.listFiles();
System.out.println("文件夹中存在的文件为:");
System.out.println("------------------");
for(int i=0;i<array.length;i++){
if(array[i].isFile()){
// only take file name
System.out.println("###"+array[i].getName()+"###");
}
}
try{
// 输入要写入的文件名
String filename=JOptionPane.showInputDialog("请输入文件名:");
File file=new File("e:\\file\\",filename);
if(!file.exists()){
file.createNewFile();
}
System.out.println("请输入你想输入的文本内容:");
b=System.in.read(bytes);
FileOutputStream fos = new FileOutputStream(file,true);
fos.write(bytes, 0, b);
fos.close();
}catch(IOException e){
e.printStackTrace();
}
}
cmd=JOptionPane.showInputDialog("请输入操作命令:\n -creat -delete -open -read -write -bye");
}
System.out.println("输入的命令为:bye");
File_os.rar_AFD算法程序_File_os.rar_file_os_二级文件系统
版权申诉
176 浏览量
2022-09-20
16:59:59
上传
评论
收藏 2KB RAR 举报
alvarocfc
- 粉丝: 105
- 资源: 1万+
最新资源
- 基于matlab实现配电网三相潮流计算方法,对几种常用的配电网潮流计算方法进行了对比分析.rar
- 基于matlab实现配电网潮流 经典33节点 前推回代法潮流计算 回代电流 前推电压 带注释.rar
- 基于matlab实现模拟退火遗传算法的车辆调度问题研究,用MATLAB语言加以实现.rar
- 基于matlab实现蒙特卡洛的的移动传感器节点定位算法仿真代码.rar
- 华中数控系统818用户说明书
- 基于matlab实现卡尔曼滤波器完成多传感器数据融合 对多个机器人的不同传感器数据进行融合估计足球精确位置.rar
- 基于matlab实现进行简单车辆识别-车辆检测.rar
- 基于JSP物流信息网的设计与实现
- 基于matlab实现车牌识别程序,和论文,自己做的,做毕业设计的可以看看 .rar
- Windows系统下安装与配置Neo4j的步骤
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈