操作系统算法 之 文件系统设计
来源:互联网 发布:黑龙江优化发展环境 编辑:程序博客网 时间:2024/05/22 08:29
本程序模拟的是Linux操作系统的文件管理命令,图形化界面使用Swing。
File.java:文件类
FileManager.java:文件管理类
Editor.java:文本编辑器
Alert.java:弹窗
Main.java:程序入口
File.java
————————————————————
public class File {private String name;private String content;public File() {}public File(String name) {this.name = name;}public File(String name, String content) {this.name = name;this.content = content;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}}————————————————————
FileManager.java
————————————————————
import java.awt.EventQueue;import java.util.LinkedHashMap;import java.util.Map;public class FileManager {Map<String,File> files = new LinkedHashMap<String,File>();public void ls(){int count = 0;;for(File file : files.values()){if(count != 0){System.out.print(" ");}System.out.print(file.getName());count++;if(count == 5){count = 0;System.out.print("\n");}}if(count != 0){System.out.print("\n");}}public void touch(String fileName){if(files.containsKey(fileName)){throw new RuntimeException("该文件已存在!");}File file = new File(fileName);files.put(fileName, file);}public void vi(final String fileName){if(!files.containsKey(fileName)){File file = new File(fileName);files.put(fileName, file);}EventQueue.invokeLater(new Runnable() {public void run() {try {Editor frame = new Editor(files.get(fileName),files);frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}public void cat(String fileName){if(!files.containsKey(fileName)){throw new RuntimeException("该文件不存在!");}String content = files.get(fileName).getContent();if(content != null){System.out.print(files.get(fileName).getContent() + "\n");}}public void rm(String fileName){if(!files.containsKey(fileName)){throw new RuntimeException("该文件不存在!");}files.remove(fileName);}}————————————————————
Editor.java
————————————————————
import java.awt.BorderLayout;import java.awt.EventQueue;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.LinkedHashMap;import java.util.Map;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.border.EmptyBorder;public class Editor extends JFrame {private JPanel contentPane;private JTextArea content;private Map<String,File> files = new LinkedHashMap<String,File>();private File file;public Editor(File file,Map<String,File> files) {this.file = file;this.files = files;setTitle(file.getName());setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 600, 400);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));contentPane.setLayout(new BorderLayout(0, 0));setContentPane(contentPane);JPanel panel = new JPanel();contentPane.add(panel, BorderLayout.NORTH);panel.setLayout(new BorderLayout(0, 0));JMenuBar menuBar = new JMenuBar();menuBar.setToolTipText("");panel.add(menuBar, BorderLayout.CENTER);JMenu mnNewMenu = new JMenu("文件");menuBar.add(mnNewMenu);JMenuItem mntmNewMenuItem_2 = new JMenuItem("保存");mntmNewMenuItem_2.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {save();}});mnNewMenu.add(mntmNewMenuItem_2);JMenuItem mntmNewMenuItem = new JMenuItem("退出");mntmNewMenuItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {close();}});mnNewMenu.add(mntmNewMenuItem);content = new JTextArea();JScrollPane scrollPane = new JScrollPane(content);contentPane.add(scrollPane, BorderLayout.CENTER);content.setText(file.getContent());setLocationRelativeTo(null);}private void save(){files.get(file.getName()).setContent(content.getText());alert("保存成功!");}private void close(){this.setVisible(false);}private void alert(final String message){EventQueue.invokeLater(new Runnable() {public void run() {try {Alert frame = new Alert(message);frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}}————————————————————
Alert.java
————————————————————
import java.awt.BorderLayout;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import javax.swing.JLabel;import javax.swing.SwingConstants;import java.awt.GridLayout;import javax.swing.JButton;import java.awt.Font;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;public class Alert extends JFrame {private JPanel contentPane;public Alert(String message) {setTitle("消息");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 200, 150);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(new GridLayout(2, 1, 0, 0));JPanel panel = new JPanel();contentPane.add(panel);panel.setLayout(new BorderLayout(0, 0));JLabel text = new JLabel("");text.setHorizontalAlignment(SwingConstants.CENTER);text.setFont(new Font("宋体", Font.PLAIN, 12));text.setText(message);panel.add(text, BorderLayout.CENTER);JPanel panel_1 = new JPanel();contentPane.add(panel_1);JButton btnNewButton = new JButton("关闭");btnNewButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {close();}});btnNewButton.setFont(new Font("宋体", Font.PLAIN, 20));panel_1.add(btnNewButton);setLocationRelativeTo(null);}private void close(){this.setVisible(false);}————————————————————
Main.java
————————————————————
import java.util.Scanner;public class Main {public static void main(String[] args) {FileManager fm = new FileManager();Scanner s = new Scanner(System.in);while(true){try {System.out.print("cmd:");String cmd = s.next();switch(cmd){case "ls":{fm.ls();};break;case "touch":{fm.touch(s.next());};break;case "vi":{fm.vi(s.next());};break;case "cat":{fm.cat(s.next());};break;case "rm":{fm.rm(s.next());};break;default:{throw new RuntimeException("未知命令");}}} catch (Exception e) {System.out.print(e.getMessage() + "\n");}}}}————————————————————
0 0
- 操作系统算法 之 文件系统设计
- 操作系统文件系统综合设计
- 操作系统之文件系统笔记
- 操作系统之文件系统
- 操作系统之文件系统
- 操作系统之文件系统笔记
- 操作系统之文件系统
- 操作系统文件系统设计实验报告
- 操作系统总结之文件系统接口
- 操作系统总结之文件系统实现
- 操作系统总结之文件系统接口
- 操作系统总结之文件系统实现
- 操作系统原理读书笔记之文件系统
- 操作系统--银行家算法设计
- 操作系统设计之哲学原理
- 操作系统之银行家算法
- 操作系统之银行家算法
- 操作系统之银行家算法
- HTML+CSS实现圆圈里面有个数字
- Ubuntu apt-get 国内源
- ZCMU-1162-松哥的素数
- SpringBoot使用笔记
- selenium+python京东自动登录及秒杀
- 操作系统算法 之 文件系统设计
- 一次编译和部署环境不一致导致的问题
- malloc free 与 new delelte的区别
- android拍照裁剪修改头像
- 递推数列
- mysql 相关字符集讲解
- Android事件总线(四)源码解析otto
- android 外部存储创建文件夹总是返回false的问题
- HttpURLConnection----Android网络编程