⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filemanager.java

📁 基于java的文件系统管理实现。模拟操作系统对文件的管理
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

import java.awt.GridBagConstraints;
import java.awt.Rectangle;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Vector;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class FileManager {

	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="10,10"

	private JPanel jContentPane = null;

	private JPanel windowsButtons = null;

	private JButton sCreate = null;

	private JButton sDelete = null;

	private JButton sChange = null;

	private JButton fCreate = null;

	private JButton fDelete = null;

	private JPanel fileButtons = null;

	private JButton fSave = null;

	private JButton fClose = null;
	
	private int openFlag = 0;

	/**
	 * This method initializes jFrame
	 * 
	 * @return javax.swing.JFrame
	 */
	private JFrame getJFrame() {
		if (jFrame == null) {
			jFrame = new JFrame();
			jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			jFrame.setSize(677, 560);
			jFrame.setLocation(200,100);
			jFrame.setContentPane(getJContentPane());
			jFrame.setTitle("文件管理系统");
			go();
		}
		return jFrame;
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getWindowsButtons(), null);
			jContentPane.add(getFileButtons(), null);
			jContentPane.add(getTextField(), null);
			jContentPane.add(getTextField2(), null);
			jContentPane.add(getCDir(), null);
			jContentPane.add(getJEnter(), null);
			jContentPane.add(getJScrollPane(), null);
			jContentPane.add(getSysPanel(), null);
			jContentPane.add(getJTextlPane1(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes windowsButtons	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getWindowsButtons() {
		if (windowsButtons == null) {
			windowsButtons = new JPanel();
			windowsButtons.setLayout(null);
			windowsButtons.setBounds(new Rectangle(78, 102, 364, 68));
			windowsButtons.add(getsCreate(), null);
			windowsButtons.add(getSDelete(), null);
			windowsButtons.add(getSChange(), null);
			windowsButtons.add(getFCreate(), null);
			windowsButtons.add(getFDelete(), null);
			windowsButtons.add(getFOpen(), null);
		}
		return windowsButtons;
	}

	Icon icon1 = new ImageIcon("sEnter.JPG");
	Icon icon2 = new ImageIcon("sCreate.JPG");
	Icon icon3 = new ImageIcon("sDelete.JPG");
	Icon icon4 = new ImageIcon("sRename.JPG");
	Icon icon5 = new ImageIcon("fCreate.JPG");
	Icon icon6 = new ImageIcon("fDelete.JPG");
	Icon icon11 = new ImageIcon("fOpen.JPG");  
	Icon icon12 = new ImageIcon("fSave.JPG");
	Icon icon13 = new ImageIcon("fClose.JPG");
	Icon icon0 = new ImageIcon("sUp.JPG");
	Icon icon00 = new ImageIcon("goTO.JPG");
	Icon icon000 = new ImageIcon("sFormat.JPG");
	Icon icon7 = new ImageIcon("sOpen.JPG");
	Icon icon8 = new ImageIcon("sSave.JPG");

	private JPanel textField = null;

	private JLabel jLabel1 = null;

	private JLabel jLabel2 = null;

	private JLabel jLabel3 = null;

	private JLabel jLabel4 = null;

	private JLabel jLabel5 = null;

	private JLabel jLabel6 = null;

	private JPanel textField2 = null;

	private JLabel jLabel12 = null;

	private JLabel jLabel13 = null;

	private JTextArea fInput = null;

	private JPanel cDir = null;

	private JLabel currentDir = null;

	private JTextField directory = null;

	private JButton upDir = null;

	private JLabel jLabel = null;

	private JLabel jLabel00 = null;

	private JPanel jEnter = null;

	private JButton sEnter = null;

	private JScrollPane jScrollPane = null;

	private JTable jTable = null;

	private JLabel jLabel7 = null;

	private JButton fOpen = null;

	private JButton sFormat = null;

	
	private JButton getsCreate() {
		if (sCreate == null) {
			sCreate = new JButton(icon2);
			sCreate.setBounds(new Rectangle(0, 6, 52, 52));
			
			//创建新目录
			sCreate.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					String name=JOptionPane.showInputDialog(null,"请输入目录名:");
					if (!name.equals(""))   //目录名不空
						if (!ifSameName(name,"dir"))   //与当前目录下同类型的文件(文件夹)不重名
							createDir(name);		   //创建新目录
						else
							JOptionPane.showMessageDialog(null,"当前目录下已有同名目录!");
					else
						JOptionPane.showMessageDialog(null,"名字不能为空!");
					//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
				}
			});
		}
		return sCreate;
	}

	private JButton getSDelete() {
		if (sDelete == null) {
			sDelete = new JButton(icon3);
			sDelete.setBounds(new Rectangle(60, 6, 52, 52));
			
			//删除目录及其下面的子目录和文件
			sDelete.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					int choice=JOptionPane.showConfirmDialog(null, "你确定要删除该文件/目录吗?","",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
					if (choice == JOptionPane.OK_OPTION)  //确定删除目录
					{
						String tempName = (String)jTable.getValueAt(jTable.getSelectionModel().getLeadSelectionIndex(), 0); //得到要删除的文件名
						String type = (String)jTable.getValueAt(jTable.getSelectionModel().getLeadSelectionIndex(), 1);   //得到要删除的文件类型
						if ( type.equals("dir") )
						{
							Integer tempDir;
							
							//找到当前目录,对其及其下面的子目录和文件都删除
							for(int i=0;i<dirCol[curDirPos].items.size();i++)
							{
								tempDir = (Integer) dirCol[curDirPos].items.elementAt(i);
								if(dirCol[tempDir].name.equals(tempName) && dirCol[tempDir].flag.equals("dir"))
									deleteDir(tempDir);  //删除目录
							}
						}
						else
						{
							JOptionPane.showMessageDialog(null,"你要删除的是文件,请按“删除文件”键!");
						}
					}
					tableRefresh(curDirPos);
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
				}
			});
		}
		return sDelete;
	}

	private JButton getSChange() {
		if (sChange == null) {
			sChange = new JButton(icon4);
			sChange.setBounds(new Rectangle(120, 6, 52, 52));
			
			//改变目录名称
			sChange.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					String name=JOptionPane.showInputDialog(null,"请输入新文件/目录名:");
					String tempName = (String)jTable.getValueAt(jTable.getSelectionModel().getLeadSelectionIndex(), 0); //得到要删除的文件名
					String type = (String) jTable.getValueAt(jTable.getSelectionModel().getLeadSelectionIndex(), 1);   //得到要删除的文件类型
					
					if (!name.equals("")) //输入名称不为空
						if (!ifSameName(name,type))  //若为文件,则没有重名文件;若为文件夹,则没有重名文件夹
						{
							Integer tempDir;
							//更改文件名称
							for(int i=0;i<dirCol[curDirPos].items.size();i++)
							{
								tempDir = (Integer) dirCol[curDirPos].items.elementAt(i);
								if(dirCol[tempDir].name.equals(tempName) && dirCol[tempDir].flag.equals(type))
									dirCol[tempDir].name = name;
							}
							tableRefresh(curDirPos);  //刷新显示视图
							//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
						}
						else
							JOptionPane.showMessageDialog(null,"当前目录下已有同名记录!");
					else
						JOptionPane.showMessageDialog(null,"名字不能为空!");
				}
			});
		}
		return sChange;
	}

	private JButton getFCreate() {
		if (fCreate == null) {
			fCreate = new JButton(icon5);
			fCreate.setBounds(new Rectangle(180, 6, 52, 52));
			
			//创建新文件
			fCreate.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					String name=JOptionPane.showInputDialog(null,"请输入文件名:");
					
					if (!name.equals(""))	//文件名非空
						if (!ifSameName(name,"file"))  //当前目录下没有重名文件
						{
							int size= Integer.parseInt(JOptionPane.showInputDialog(null,"请输入文件大小(Byte):  (大于0,小于100,000)"));
							if (size>0 && size<100000)  //文件大小符合要求
							{
								createFile(name,size); //创建新文件
							}
							else 
								JOptionPane.showMessageDialog(null,"你输入的大小不正确!");
						
						}
						else
							JOptionPane.showMessageDialog(null,"当前目录下已有同名文件!");
					else
						JOptionPane.showMessageDialog(null,"名字不能为空!");
					//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
				}
				
			});
		}
		return fCreate;
	}

	private JButton getFDelete() {
		if (fDelete == null) {
			fDelete = new JButton(icon6);
			fDelete.setBounds(new Rectangle(240, 6, 52, 52));
			
			//删除文件
			fDelete.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					int choice=JOptionPane.showConfirmDialog(null, "你确定要删除该文件/目录吗?","",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
					if (choice == JOptionPane.OK_OPTION)
					{
						String tempName = (String)jTable.getValueAt(jTable.getSelectionModel().getLeadSelectionIndex(), 0); //含义同 删除文件夹 中的语句
						String type = (String)jTable.getValueAt(jTable.getSelectionModel().getLeadSelectionIndex(), 1);  //含义同 删除文件夹 中的语句
						if (type.equals("file") )	//含义同 删除文件夹 中的语句
						{
							Integer tempDir;
							//找到对应的FCB,进行删除
							for(int i=0;i<dirCol[curDirPos].items.size();i++)
							{
								tempDir = (Integer) dirCol[curDirPos].items.elementAt(i);  
								if(dirCol[tempDir].name.equals(tempName) && dirCol[tempDir].flag.equals("file"))
									deleteFile(tempDir);   //删除文件
							}
						}
						else
						{
							JOptionPane.showMessageDialog(null,"你要删除的是目录,请按“删除目录”键!");
						}
					}
					tableRefresh(curDirPos);
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
				}
			});
		}
		return fDelete;
	}

	private JPanel getFileButtons() {
		if (fileButtons == null) {
			fileButtons = new JPanel();
			fileButtons.setLayout(null);
			fileButtons.setBounds(new Rectangle(494, 149, 128, 65));
			fileButtons.add(getFSave(), null);
			fileButtons.add(getFClose(), null);
		}
		return fileButtons;
	}

	private JButton getFSave() {
		if (fSave == null) {
			fSave = new JButton(icon12);
			fSave.setBounds(new Rectangle(3, 8, 52, 52));
			fSave.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					fileSave();    //保存当前文件
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
				}
			});
		}
		return fSave;
	}

	private JButton getFClose() {
		if (fClose == null) {
			fClose = new JButton(icon13);
			fClose.setBounds(new Rectangle(63, 8, 52, 52));
			fClose.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					fileClose();	//关闭当前文件
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
				}
			});

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -