基于java的文件资源管理器

删除文件

	 public void deleteFile(File file) {
		 if(file!=null)
		    if (file.exists()) {//判断文件是否存在  
		     if (file.isFile()) {//判断是否是文件  
		      file.delete();//删除文件   
		     } else if (file.isDirectory()) {//否则如果它是一个目录  
		      File[] files = file.listFiles();//声明目录下所有的文件 files[];  
		      for (int i = 0;i < files.length;i ++) {//遍历目录下所有的文件  
		       this.deleteFile(files[i]);//把每个文件用这个方法进行迭代  
		      }  
		      file.delete();//删除文件夹  
		     }  
		    } else {  
		     System.out.println("所删除的文件不存在");  
		    }  
		   }  
复制文件

 public static void copy(File resFile, File objFolderFile) throws IOException { 
         if (!resFile.exists()) return; 
         if (!objFolderFile.exists()) objFolderFile.mkdirs(); 
         if (resFile.isFile()) { 
                 File objFile = new File(objFolderFile.getPath() + File.separator + resFile.getName()); 
                 //复制文件到目标地 
                 InputStream ins = new FileInputStream(resFile); 
                 FileOutputStream outs = new FileOutputStream(objFile); 
                 byte[] buffer = new byte[1024 * 512]; 
                 int length; 
                 while ((length = ins.read(buffer)) != -1) { 
                         outs.write(buffer, 0, length); 
                 } 
                 ins.close(); 
                 outs.flush(); 
                 outs.close(); 
         } else { 
                 String objFolder = objFolderFile.getPath() + File.separator + resFile.getName(); 
                 File _objFolderFile = new File(objFolder); 
                 _objFolderFile.mkdirs(); 
                 for (File sf : resFile.listFiles()) { 
                         copy(sf, new File(objFolder)); 
                 } 
         } 
	 } 
行操作模式
public class LineMode  {
	private Desktop desktop = Desktop.getDesktop();
	public void run() {
		Frame frame= new Frame("行操作模式");
		frame.setBounds(420,350,545,400);
		frame.setResizable(false);
		frame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent evt){
				frame.setVisible(false);
			}
		});
		
		
		TextArea remind = new TextArea(20,70);
		Button updata = new Button("执行");
		Color co=new Color(15790320);
		remind.setBackground(co);
		JTextField Content;
		Content = new JTextField();
		Panel north = new Panel();
		frame.add(BorderLayout.NORTH,north);
	 	north.setLayout(new GridLayout(2,1)); 
		north.add(Content);	
		north.add(updata);
		frame.add(remind,BorderLayout.PAGE_END);
		remind.setText("\n");
//		remind.setText("\n  提醒:\n");
//		remind.append("  打开操作: open(OPEN)"+" 目录\n");
//		remind.append("  删除操作: delete(DELETE)"+" 目录\n");	
//		remind.append("  剪贴操作: cut(CUT)"+" 源目录 "+"目标目录\n");
//		remind.append("  复制操作: copy(COPY)"+" 源目录 "+"目标目录\n");	
		
		updata.addActionListener( new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String Count[] = Content.getText().split("\\s+"); 
				Calendar c = Calendar.getInstance();
				int flag=0;
				if(Count.length==2)
				{
					//删除行操作
					if(Count[0].compareTo("delete")==0||Count[0].compareTo("DELETE")==0)
						{
							flag=1;
							remind.append("  "+c.getTime()+"\n  删除 "+Count[1]+" 成功\n");
							File file = new File(Count[1]);
							deleteFile(file);
							
						}
					//打开行操作
					if(Count[0].compareTo("open")==0||Count[0].compareTo("OPEN")==0)
						{
							flag=1;
							File file = new File(Count[1]);
							try {
								desktop.open(file);
							} catch (IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							remind.append("  "+c.getTime()+"\n  打开 "+Count[1]+" 成功\n");
							
							
						}
					
				}
				
				if(Count.length==3)
				{
					//复制行操作
					if(Count[0].compareTo("copy")==0||Count[0].compareTo("COPY")==0)
						{
						flag=1;
						File sfile = new File(Count[1]);
						File tfile = new File(Count[2]);
						try {
							copy(sfile, tfile);
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						remind.append("  "+c.getTime()+"\n  复制 "+Count[1]+" 成功\n");
						
						
						}
					if(Count[0].compareTo("cut")==0||Count[0].compareTo("CUT")==0)
						{
						flag=1;
						File sfile = new File(Count[1]);
						File tfile = new File(Count[2]);
						try {
							copy(sfile, tfile);
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						deleteFile(sfile);
						remind.append("  "+c.getTime()+"\n  剪贴 "+Count[1]+" 成功\n");
						
						}
					
				}
				if(flag==0)
					remind.append("  输入非法,请重新输入\n");	
			}
			
		});
		//if(Content.getText())
		frame.setVisible(true);
		
	}
附上源码:http://download.csdn.net/download/cx_mors/10185996 

有需要的可以去下载,不希望用积分的可以留言,看到的话会给你们发邮件!



你可能感兴趣的:(java)