把之前写的一个小东西代码分享出来

大家接触到流的输出输入和GUI就会自己写了,其实soeasy 所以不好意思拿出来,毕竟是小白,主要还是因为懒,FileDialog也懒得弄了,讲道理界面太丑,加了dialog也挺low,自己想留着用的可以自己去完善,基本上加些东西就ok了。如果你们复制过去发现没有图片,请不要紧张,因为图片在我这啊~~你们自己照着路径修改就好了,群里面有打包了的有图片的,有需要二次元 图片的可以私聊我哟。。。。。

启动类:什么都木有,哈哈哈

 1 package practice.AsceticJ;
 2 
 3 /**
 4  * @author AsceticJ
 5  * @date 2017年2月26日 下午5:43:45
 6  * @version 1.1
 7  * @TODO Description
 8  */
 9 public class FilesManager
10 {
11     public static void main(String[] args)
12     {
13         new GUI();
14     }
15 }

图形用户界面类:

就是画图和添加事件监听

  1 package practice.AsceticJ;
  2 
  3 import java.awt.Color;
  4 import java.awt.Cursor;
  5 import java.awt.Dimension;
  6 import java.awt.FlowLayout;
  7 import java.awt.Font;
  8 import java.awt.GridLayout;
  9 import java.awt.Image;
 10 import java.awt.Label;
 11 import java.awt.Toolkit;
 12 import java.awt.event.ActionEvent;
 13 import java.awt.event.ActionListener;
 14 import java.awt.event.FocusAdapter;
 15 import java.awt.event.FocusEvent;
 16 import java.awt.event.WindowAdapter;
 17 import java.awt.event.WindowEvent;
 18 import java.io.File;
 19 import java.util.ArrayList;
 20 
 21 import javax.swing.BorderFactory;
 22 import javax.swing.ImageIcon;
 23 import javax.swing.JButton;
 24 import javax.swing.JFrame;
 25 import javax.swing.JLabel;
 26 import javax.swing.JPanel;
 27 import javax.swing.JScrollBar;
 28 import javax.swing.JScrollPane;
 29 import javax.swing.JTextArea;
 30 import javax.swing.JTextField;
 31 
 32 //import javax.swing.JComponent;
 33 /**
 34  * @author AsceticJ
 35  * @date 2017年2月25日 下午11:22:48
 36  * @version 1.0
 37 
 38  * @TODO Description
 39  */
 40 public class GUI
 41 {    
 42     public GUI(){
 43         setGUI();
 44     }
 45     
 46     private JFrame jf;
 47     private JPanel jpaMain,jpaMain1,jpaMain2,jpaMain3;
 48     
 49     private Label lbHead;//窗体内标题
 50     private Label lbOrg,lbNew;
 51     
 52     private JTextArea jta1;
 53     private JTextArea jta2;
 54     
 55     private JTextField jtf1 = new JTextField("请输入源文件夹路径");
 56     private JTextField jtf2 = new JTextField("原前缀");
 57     private JTextField jtf3 = new JTextField("原关键词");
 58     private JTextField jtf4 = new JTextField("原后缀");
 59     private JTextField jtf5 = new JTextField("请输入目标文件夹路径");
 60     private JTextField jtf6 = new JTextField("目标前缀");
 61     private JTextField jtf7 = new JTextField("目标关键词");
 62     private JTextField jtf8 = new JTextField("目标后缀");
 63     
 64     private JButton jbtn1 = new JButton("查找");
 65     private JButton jbtn2 = new JButton("查找全部");
 66     private JButton jbtn3 = new JButton("复制");
 67     private JButton jbtn4 = new JButton("删除");
 68     private JButton jbtn5 = new JButton("重命名");
 69     private JButton jbtn6 = new JButton("撤销");
 70     
 71     private String oPath,oPre,oKey,oSuf,nPre,nKey,nSuf,nPath;//存放关键词的字符串
 72     private Boolean bFlag = false;
 73     private String fSwitch ="";
 74     private String eventSwitch ="";
 75     private String [] matchFiles,matchFilesName;
 76     private String [] renamedName = {""};
 77     private ArrayList<String> matchAllFiles;
 78     
 79     //配置各个组件的参数
 80     public void setGUI()
 81     {
 82         jf = new JFrame("Files Manager");
 83         //首先设置jf的布局管理器
 84         jf.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 15));
 85         //配置整个窗体尺寸,窗居中,不可改变大小
 86         jf.setSize(1000, 600);
 87         jf.setLocationRelativeTo(null);
 88         jf.setResizable(false);
 89         
 90         //设置标题栏的Icon        
 91         Image icon = Toolkit.getDefaultToolkit().getImage("img/1.png");// 设置左上角logo图标
 92         jf.setIconImage(icon);
 93         //设置背景图片
 94         String bgPath = "img/bg4.jpg";//背景图路径
 95         ImageIcon bgImg = new ImageIcon(bgPath);//将路径指向的图片对象new出来
 96         JLabel bgJlb = new JLabel(bgImg,JLabel.CENTER);//创建一个具有水平居中的图片的label
 97         bgJlb.setBounds(0, 0, 1000, 600);//设置bgJlb大小
 98         /*下面将bgJlb放到jf的内容容器中,方式一
 99         Container bgImgCon= jf.getContentPane();//获取jf的内容对象,注意:jf.getContentPane返回一个container对象
100         ((JComponent) bgImgCon).setOpaque(false);//设置为不透明,因为container类没有这个方法,故将其转换。
101         *///方式二
102         JPanel bgImgPan = (JPanel) jf.getContentPane();//获取jf的内容对象
103         bgImgPan.setOpaque(false);//设置为不透明
104         jf.getLayeredPane().add(bgJlb, new Integer(Integer.MIN_VALUE));//将图片Jlabel设置成背景,也就是最底层
105         
106         //设置窗体内的大标题
107         lbHead = new Label("Files Manager",Label.CENTER);
108         lbHead.setForeground(Color.black);
109         lbHead.setBackground(new Color(153,153,153));
110         lbHead.setFont(new Font("黑体", Font.PLAIN, 36));
111         lbHead.setPreferredSize(new Dimension(400, 60));
112         jf.add(lbHead); 
113         
114         //设置内容布局
115         jpaMain = new JPanel();
116         GridLayout gp = new GridLayout(1,3); 
117         jpaMain.setLayout(gp);
118         jpaMain.setOpaque(false);
119         jpaMain.setPreferredSize(new Dimension(840, 450));
120         
121         Color bdColor = Color.white;
122         Font h2Font = new Font("黑体", Font.PLAIN, 20);
123         Font taFont = new Font("宋体",Font.BOLD , 14);
124         
125         //源文件预览框
126         jpaMain1 = new JPanel();
127         jpaMain1.setOpaque(false);
128         jpaMain1.setBorder(BorderFactory.createLineBorder(bdColor, 2));
129         jpaMain1.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
130         //预览框标题
131         lbOrg =new Label("sourceFiles", Label.CENTER);
132         lbOrg.setForeground(Color.white);
133         lbOrg.setBackground(new Color(40,64,90));
134         lbOrg.setFont(h2Font);
135         lbOrg.setPreferredSize(new Dimension(276, 30));
136         jpaMain1.add(lbOrg);
137         //设置预览文本域
138         jta1 = new JTextArea();
139         jta1.setForeground(Color.red);
140         jta1.setOpaque(false);
141         jta1.setFont(taFont);
142         jta1.setLineWrap(true);
143         jta1.setEditable(false);
144         //给文本框设置滚动条
145         JScrollPane jsp1 = new JScrollPane(jta1);
146         jsp1.setPreferredSize(new Dimension(278,419));
147         //设置滚动条部分组件和视口透明
148         jsp1.setOpaque(false);
149         jsp1.getViewport().setOpaque(false);
150         JScrollBar jscr1 = jsp1.getVerticalScrollBar();
151         jscr1.setOpaque(false);
152         jscr1.getComponent(0).setVisible(false);
153         jscr1.getComponent(1).setVisible(false);
154         jpaMain1.add(jsp1);        
155         jpaMain.add(jpaMain1);
156                 
157         //目标文件预览框
158         jpaMain2 = new JPanel();
159         jpaMain2.setOpaque(false);
160         jpaMain2.setBorder(BorderFactory.createLineBorder(bdColor, 2));
161         jpaMain2.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
162         //预览框标题
163         lbNew =new Label("newFiles", Label.CENTER);
164         lbNew.setForeground(Color.white);
165         lbNew.setBackground(new Color(40,64,90));
166         lbNew.setFont(h2Font);
167         lbNew.setPreferredSize(new Dimension(276, 30));
168         jpaMain2.add(lbNew);
169         //文件预览文本域
170         jta2 = new JTextArea("声明:本程序只是本人为了实现筛选复制和重命名测试用的,部分功能可以用Windows自带的功能实现。\n\n操作说明:\n 1、点击查找或者查找全部\n 2、确认你选择的文件是否正确\n 3、路径输入格式为root:\\fold\\fold\n 4、如果想将文件保存在新的文件夹中,可以在目标路径加上\\新建文件夹名 \n 5、撤销只能撤销上一步");
171         jta2.setOpaque(false);
172         jta2.setForeground(Color.blue);
173         jta2.setFont(taFont);
174         jta2.setLineWrap(true);
175         jta2.setEditable(false);
176         //给文本框设置滚动条
177         JScrollPane jsp2 = new JScrollPane(jta2);
178         jsp2.setPreferredSize(new Dimension(278,419));
179         //设置滚动条部分组件和视口透明
180         jsp2.setOpaque(false);
181         jsp2.getViewport().setOpaque(false);
182         JScrollBar jscr2 = jsp2.getVerticalScrollBar();
183         jscr2.setOpaque(false);
184         jscr2.getComponent(0).setVisible(false);
185         jscr2.getComponent(1).setVisible(false);
186         jpaMain2.add(jsp2);
187         jpaMain.add(jpaMain2);  
188         
189         //设置菜单栏
190         jpaMain3 = new JPanel();
191         jpaMain3.setOpaque(false);
192         jpaMain3.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 30));
193         //给所有的文本框参数初始化
194         initJTF(jtf1, 276);
195         initJTF(jtf2, 92);
196         initJTF(jtf3, 92);
197         initJTF(jtf4, 92);
198         initJTF(jtf5, 276);
199         initJTF(jtf6, 92);
200         initJTF(jtf7, 92);
201         initJTF(jtf8, 92);
202         //将文本框加到jpaMain3
203         jpaMain3.add(jtf1);
204         jpaMain3.add(jtf2);
205         jpaMain3.add(jtf3);
206         jpaMain3.add(jtf4);
207         jpaMain3.add(jtf5);
208         jpaMain3.add(jtf6);
209         jpaMain3.add(jtf7);
210         jpaMain3.add(jtf8);
211         
212         //初始化按钮参数
213         initJBtn(jbtn1, 90);
214         initJBtn(jbtn2, 90);
215         initJBtn(jbtn3, 90);
216         initJBtn(jbtn4, 90);
217         initJBtn(jbtn5, 90);
218         initJBtn(jbtn6, 90);
219       //将按钮加到jpaMain3
220         jpaMain3.add(jbtn1);
221         jpaMain3.add(jbtn2);
222         jpaMain3.add(jbtn3);
223         jpaMain3.add(jbtn4);
224         jpaMain3.add(jbtn5);
225         jpaMain3.add(jbtn6);
226         
227         jpaMain.add(jpaMain3);
228         jf.add(jpaMain);
229         //调用事件监听
230         gEvent();
231         jf.setVisible(true);
232 //        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
233     }
234         
235     private void gEvent()
236     {               
237         jf.addWindowListener(new WindowAdapter() {
238             public void windowClosing(WindowEvent e) {
239                 FilesEvent.clearTemp();
240                 System.exit(0);
241             }
242         });
243 
244         //源文件路径文本框添加键盘失焦事件,以便快速检查用户输入的正确性
245         jtf1.addFocusListener(new FocusAdapter()
246         {
247             public void focusLost(FocusEvent e)
248             {
249                 oPath = jtf1.getText();
250                 if (oPath.equals("请输入源文件夹路径"))
251                 {
252                     bFlag=false;
253                     return;
254                 }
255                 File oFiles = new File(oPath);
256                 bFlag = oFiles.exists()?true:false;
257                 if(!bFlag)jtf1.setText("您输入的路径有误!");
258                 return;
259             }
260         });
261         //给查找按钮添加点击事件
262         jbtn1.addActionListener(new ActionListener(){
263             @Override
264             public void actionPerformed(ActionEvent e)
265             {
266                 if(!bFlag) return;
267                 getKeyWords(true);
268                 FilesEvent.setKeyWords(oPre,oKey,oSuf);
269                 matchFiles = FilesEvent.filesSearch(oPath);
270                 int len = matchFiles.length;
271                 matchFilesName = new String [len];
272                 jta1.setText("");
273                 if(matchFiles[0].isEmpty()){
274                     jta1.setText("找不到您要的文件,请检查关键词是否正确!");
275                 }else{                
276                     for (int i=0;i<matchFiles.length;i++)
277                     {
278                         int lIndex = matchFiles[i].lastIndexOf('\\');//截取文件地址上的文件名
279                         matchFilesName[i] = matchFiles[i].substring(lIndex+1);
280                         jta1.append((i+1) +"."+matchFilesName[i]+"\n");
281                     }
282                 }
283                 fSwitch = "searchCurrent";
284                 eventSwitch = "searchCurrent";//给查找按钮标记为"searchCurrent"    
285             }
286         });
287         //给查找全部添加点击事件
288         jbtn2.addActionListener(new ActionListener()
289         {
290             @Override
291             public void actionPerformed(ActionEvent e)
292             {
293                 if(!bFlag) return;
294                 getKeyWords(true);
295                 FilesEvent.setKeyWords(oPre,oKey,oSuf);
296                 matchAllFiles = new ArrayList<String>();
297                 FilesEvent.filesSearchAll(oPath,matchAllFiles);
298                 jta1.setText("");
299                 if(matchAllFiles.isEmpty()){
300                     jta1.setText("找不到您要的文件,请检查关键词是否正确!");
301                 }else{
302                     int index = 1;
303                     for (String maf : matchAllFiles)
304                     {
305                         int lIndex = maf.lastIndexOf('\\');
306                         jta1.append(index+"."+maf.substring(lIndex+1)+"\n");
307                         index++;
308                     }
309                 }
310                 fSwitch = "searchAll";
311                 eventSwitch = "searchAll";//给查找按钮标记为"searchAll"
312             }
313         });
314         //给复制按钮添加事件
315         jbtn3.addActionListener(new ActionListener()
316         {            
317             @Override
318             public void actionPerformed(ActionEvent e)
319             {
320                 if(fSwitch.equals("")){return;}
321                 jta2.setText("");
322                 nPath = jtf5.getText();
323                 switch (fSwitch)
324                 {
325                 case "searchCurrent":
326                     int j = 1;
327                     for(String fPath:matchFiles){
328                         String copyTips = FilesEvent.filesCopy(fPath, nPath);
329                         jta2.append(j+"."+copyTips+"\n");
330                         j++;
331                     }
332                     jta2.append("已经完成复制操作!\n");
333                     break;
334                 case "searchAll":
335                     int k = 1;
336                     for(String fPath:matchAllFiles){
337                         String copyTips = FilesEvent.filesCopy(fPath, nPath);
338                         jta2.append(k+"."+copyTips+"\n");
339                         k++;
340                     }
341                     jta2.append("已经完成复制操作!\n");
342                     break;
343                 }
344                 eventSwitch = "filesCopy";//给复制按钮标记为"filesCopy"
345             }
346         });
347         //给删除按钮添加事件
348         jbtn4.addActionListener(new ActionListener()
349         {
350             @Override
351             public void actionPerformed(ActionEvent e)
352             {
353                 if(fSwitch.equals("")){return;}
354                 jta2.setText("");
355                 File tempFolder =  new File("C:\\Temp\\FilesManager");
356                 if(!tempFolder.exists()){
357                     tempFolder.mkdirs();
358                 }
359                 String [] tempName = tempFolder.list();
360                 if(0!=tempName.length){
361                     File [] templist = tempFolder.listFiles();
362                     for (File tf : templist)
363                     {
364                         tf.delete();//先清空缓存文件夹
365                     }
366                 }
367                 
368                 if(fSwitch.equals("searchCurrent"))
369                 {
370                     int j = 1;
371                     for(String fPath:matchFiles){
372                         String tips;
373                         if (!new File(fPath).exists())
374                         {
375                             tips = ".您要删除的文件不存在!\n";
376                         }else {
377                             FilesEvent.filesDelete(fPath);
378                             tips = ".删除成功!\n";
379                         }
380                         jta2.append(j+tips);
381                         j++;    
382                     }
383                     jta2.append("已经完成删除操作!\n");
384                 }else{
385                     int k = 1;
386                     for(String fPath:matchAllFiles){
387                         String tips;
388                         if (!new File(fPath).exists())
389                         {
390                             tips = ".您要删除的文件不存在!\n";
391                         }else {
392                             FilesEvent.filesDelete(fPath);
393                             tips = ".删除成功!\n";
394                         }
395                         jta2.append(k+tips);
396                         k++;
397                     }
398                     jta2.append("已经完成删除操作!\n");
399                 }
400                 eventSwitch = "filesDelete";//给删除按钮标记为"filesDelete"
401             }
402         });
403         //给重命名按钮添加事件,自带的rename在不同文件系统下会失败,所以我们先复制
404         jbtn5.addActionListener(new ActionListener()
405         {
406             @Override
407             public void actionPerformed(ActionEvent e)
408             {
409                 if(fSwitch.equals("")){return;}
410                 jta2.setText("");
411                 nPath = jtf5.getText();
412                 getKeyWords(true);
413                 FilesEvent.setKeyWords(oPre, oKey, oSuf);
414                 getKeyWords(false);
415                 FilesEvent.setNkeyWords(nPre, nKey, nSuf);
416                 switch (fSwitch)
417                 {
418                 case "searchCurrent":
419                     int j = 1;
420                     renamedName = new String [matchFiles.length];
421                     for(String fPath:matchFiles){
422                         renamedName[j-1]=FilesEvent.filesRename(fPath,nPath);
423                         jta2.append(j+"."+FilesEvent.filesRename(fPath,nPath)+"\n");
424                         j++;
425                     }
426                     jta2.append("已经完成重命名操作!\n");
427                     break;
428                 case "searchAll":
429                     int k = 1;
430                     renamedName = new String [matchAllFiles.toArray().length];
431                     for(String fPath:matchAllFiles){
432                         renamedName[k-1] = FilesEvent.filesRename(fPath,nPath);    
433                         jta2.append(k+"."+FilesEvent.filesRename(fPath,nPath)+"\n");
434                         k++;
435                     }
436                     jta2.append("已经完成重命名操作!\n");
437                     break;
438                 }
439                 eventSwitch = "filesRename";//给重命名按钮标记为"filesRename"
440             }
441         });
442         jbtn6.addActionListener(new ActionListener()
443         {
444             @Override
445             public void actionPerformed(ActionEvent e)
446             {
447                 if(fSwitch.equals("")){return;}
448                 if(eventSwitch.equals(fSwitch)||eventSwitch.equals("")){
449                     jta1.setText("");
450                 }else if(eventSwitch.equals("eventCancel")) {
451                     jta2.setText("亲,你已经撤销过一次了哟");
452                     return;
453                 }else {
454                     if(fSwitch.equals("searchCurrent")){
455                         int a =0;
456                         for (String fpath : matchFiles)
457                         {
458                             FilesEvent.eventCancel(eventSwitch,nPath,fpath,renamedName[a]);
459                             a = renamedName[0].isEmpty()?0:a+1;
460                         }
461                         
462                     }else{
463                         int b = 0;
464                         for (String fpath : matchAllFiles)
465                         {
466                             FilesEvent.eventCancel(eventSwitch,nPath,fpath,renamedName[b]);
467                             b = renamedName[0].isEmpty()?0:b+1;
468                         }
469                     }
470                 }
471                 jta2.setText("已经成功撤销上步操作!");
472                 eventSwitch = "eventCancel";
473             }
474         });
475     }
476     
477     //文本框初始化参数 
478     private void initJTF(JTextField JTF,int width)
479     {
480         JTF.setPreferredSize(new Dimension(width, 40));
481         JTF.setHorizontalAlignment(JTextField.CENTER);
482         JTF.setFont(new Font("宋体", Font.PLAIN, 16));
483         JTF.setBorder(BorderFactory.createLineBorder(Color.white, 2));
484         JTF.setOpaque(false);
485         JTF.setForeground(Color.white);
486     }
487     //按钮初始化参数
488     private void initJBtn(JButton Jbtn,int width)
489     {
490         Jbtn.setPreferredSize(new Dimension(width, 40));
491         Jbtn.setHorizontalAlignment(JButton.CENTER);
492         Jbtn.setFont(new Font("黑体", Font.BOLD, 16));
493         Jbtn.setForeground(Color.black);
494         Jbtn.setBorder(BorderFactory.createLineBorder(Color.blue, 3));
495         Jbtn.setCursor(new Cursor(Cursor.HAND_CURSOR));
496     }
497     //获取文本框的关键词
498     private void getKeyWords(Boolean kFlag)
499     {
500         if(kFlag){
501             oPre = jtf2.getText().equals("原前缀")?"":jtf2.getText();
502             oKey = jtf3.getText().equals("原关键词")?"":jtf3.getText();
503             oSuf = jtf4.getText().equals("原后缀")?"":jtf4.getText();
504         }else {
505             nPre = jtf6.getText().equals("目标前缀")?"":jtf6.getText();
506             nKey = jtf7.getText().equals("目标关键词")?"":jtf7.getText();
507             nSuf = jtf8.getText().equals("目标后缀")?"":jtf8.getText();
508         }
509     }    
510 }

文件事件处理类:

就是对监听的事件 进行相应的处理

  1 package practice.AsceticJ;
  2 
  3 import java.io.BufferedInputStream;
  4 import java.io.BufferedOutputStream;
  5 import java.io.File;
  6 import java.io.FileInputStream;
  7 import java.io.FileNotFoundException;
  8 import java.io.FileOutputStream;
  9 import java.io.IOException;
 10 import java.util.ArrayList;
 11 import java.util.Arrays;
 12 
 13 
 14 /**
 15  * @author AsceticJ
 16  * @date 2017年2月26日 下午4:54:41
 17  * @version 1.0
 18 
 19  * @TODO Description
 20  */
 21 public class FilesEvent
 22 {
 23     private static String [] keywords = new String [3];
 24     private static String [] nkeywords = new String [3];
 25     public static void setKeyWords(String oPre,String oKey,String oSuf)
 26     {
 27         keywords[0] = oPre; 
 28         keywords[1] = oKey;
 29         keywords[2] = oSuf;
 30         //还需要考虑用户在首尾殊荣空格的问题
 31     }
 32     public static void setNkeyWords(String nPre,String nKey,String nSuf)
 33     {
 34         nkeywords[0] = nPre; 
 35         nkeywords[1] = nKey;
 36         nkeywords[2] = nSuf;
 37         //还需要考虑用户在首尾殊荣空格的问题
 38     }
 39     //关闭时清空缓存文件夹
 40     public static void clearTemp()
 41     {
 42         File tempFolder =  new File("C:\\Temp\\FilesManager");
 43         if(!tempFolder.exists())return;
 44         File [] templist = tempFolder.listFiles();
 45         for (File tf : templist)
 46         {
 47             tf.delete();
 48         }
 49     }
 50     
 51     
 52     public static String [] filesSearch(String path)
 53     {
 54         File file = new File(path);
 55         File [] files = file.listFiles();
 56         StringBuffer fileSb = new StringBuffer();
 57         for (File f : files)
 58         {
 59             String fName = f.getName();
 60             int i = fName.indexOf(keywords[1]);
 61             int j = fName.length()-1;
 62             if(fName.startsWith(keywords[0])&&fName.endsWith(keywords[2])&&((i>0&&i<j)||keywords[1].equals(""))){
 63                 fileSb.append(f+"?");
 64             }
 65         }
 66         String [] matchFiles = fileSb.toString().split("?");
 67         return matchFiles;
 68     }
 69     
 70     public static ArrayList<String> filesSearchAll(String path,ArrayList<String> fileName)
 71     {
 72         File file = new File(path);
 73         File [] files = file.listFiles();//path当前目录下的文件对象数组
 74         String [] matchTemp = filesSearch(path);//获取当前文件夹下匹配的文件
 75         if(!matchTemp[0].isEmpty())
 76         fileName.addAll(Arrays.asList(matchTemp));//将matchTemp数组放到fileName列表中
 77         //进行迭代,将子目录下的文件名数组全部放到fileName中
 78         for(File f:files)
 79         {
 80             if(f.isDirectory())
 81             {
 82                 filesSearchAll(f.getAbsolutePath(),fileName);
 83             }
 84         }
 85         return fileName;
 86     }
 87     
 88     public static String filesCopy(String fPath,String newPath)
 89     {
 90         BufferedOutputStream bos = null;//创建字节缓冲输出流和输入流
 91         BufferedInputStream bis = null;
 92         File des = new File(newPath);
 93         if(!des.exists()){
 94             des.mkdirs();
 95         }
 96         File src = new File(fPath);
 97         if(src.isDirectory()){
 98             newPath +="\\"+src.getName();
 99             des = new File(newPath);
100             if(!des.exists()){
101                 des.mkdir();
102             }
103         }else{
104             try{
105                 bis = new BufferedInputStream(new FileInputStream(fPath));//字节缓冲输出流和输入流初始化
106                 bos = new BufferedOutputStream(new FileOutputStream(newPath+"\\"+src.getName()));
107                 byte [] buf = new byte [1024];//设置字节缓冲流和字节长度
108                 int len = bis.read(buf, 0, 1024);//将源文件数据读到缓冲流中
109                 while (-1!=len)
110                 {
111                     bos.write(buf, 0, len);//将缓冲流中的数据写入目标文件中
112                     len = bis.read(buf);
113                 }
114                 //读写完毕,将缓冲流清空,关闭输入输出流
115                 bos.flush();
116                 bis.close();
117                 bos.close();
118             }catch (FileNotFoundException e) {
119                 return "没有找到文件!";
120             }catch (IOException e){
121                 return "文件读写错误!";
122             }
123         }
124         return "文件复制完成!";
125     }
126     
127     public static String filesRename(String macthpath,String npath)
128     {
129         filesCopy(macthpath, npath+"\\temp");
130         File ofile = new File(macthpath);
131         String fname = ofile.getName();
132         File file = new File(npath+"\\temp\\"+fname);    
133         String newName=fname.replaceFirst(keywords[0], nkeywords[0]);
134         newName=keywords[1].equals("")?newName:newName.replaceFirst(keywords[1], nkeywords[1]);
135         newName=keywords[2].equals("")?newName.concat(nkeywords[2]):newName.replaceFirst(keywords[2], nkeywords[2]);
136         Boolean confirm = file.renameTo(new File(npath+"\\"+newName));
137         ofile.delete();
138         if(!confirm){file.delete();}//如果目标文件夹下存在和重命名的文件同名  则不对其进行修改。
139         File tempFile = new File(npath+"\\temp");
140         tempFile.delete();
141         return newName;
142     }
143     public static void filesDelete(String fPath)
144     {
145         filesCopy(fPath, "C:\\Temp\\FilesManager");//删除前备份到缓存文件夹,方便还原
146         File file = new File(fPath);
147         if(file.exists()){
148             file.delete();
149         }else {
150             return;
151         }
152         
153     }
154     
155     public static void eventCancel(String evStr,String npath,String fPath,String reName)
156     {
157         int lIndex = fPath.lastIndexOf('\\');
158         String fileName = fPath.substring(lIndex+1);
159         String opath = fPath.substring(0,lIndex);
160         String newpath = npath+"\\"+fileName;
161         switch (evStr)
162         {
163         case "filesCopy":
164             File des = new File(newpath);
165             des.delete();
166             break;
167         case "filesDelete":
168             filesCopy("C:\\Temp\\FilesManager\\"+fileName, opath);
169             break;
170         case "filesRename":
171             filesCopy(npath+"\\"+reName, opath);
172             File file1 = new File(opath+"\\"+reName);
173             File file2 = new File(npath+"\\"+reName);
174             file1.renameTo(new File(fPath));
175             file2.delete();
176             break;
177         }
178     }
179     
180 }

因为之前设置的缓存文件夹在D盘,考虑到用的是网吧的无盘系统,就临时对部分代码做了修改,可能会有错  你们可以自己修改   我相信你们可以的。

 

posted @ 2017-03-12 10:58  Ascetic  阅读(283)  评论(0编辑  收藏  举报