java 文件与文件夹拷贝、重命名、文件搜索

  1. java文件搜索  
  2. public void getDir(String strPath) throws Exception  
  3.     {  
  4.         try  
  5.         {  
  6.             File f=new File(strPath);  
  7.             if(f.isDirectory())  
  8.             {  
  9.                 File[] fList=f.listFiles();  
  10.                 for(int j=0;j<fList.length;j++)  
  11.                 {  
  12.                     if(fList[j].isDirectory())  
  13.                     {  
  14.                         System.out.println(fList[j].getPath());  
  15.                         getDir(fList[j].getPath()); //在getDir函数里面又调用了getDir函数本身   
  16.                     }  
  17.                 }  
  18.                 for(int j=0;j<fList.length;j++)  
  19.                 {  
  20.                       
  21.                     if(fList[j].isFile())  
  22.                     {  
  23.                         System.out.println(fList[j].getPath());//把这话换成你要处理的语句.比如 fList[j].substring(fList[j].length-3,3)=="txt"  
  24.                     }  
  25.                       
  26.                 }  
  27.             }  
  28.         }  
  29.         catch(Exception e)  
  30.         {  
  31.             e.printStackTrace();  
  32.         }  
  33.     }  
  34. java 文件与文件夹拷贝  
  35. File fileOld = new File(pathOld);  
  36.         File fileNew = new File(pathNew);  
  37.         if(fileOld.exists()){  
  38.             try {  
  39.                 FileInputStream fis = new FileInputStream(fileOld);  
  40.                 FileOutputStream fos = new FileOutputStream(fileNew);  
  41.                 int read = 0;  
  42.                 while ((read = fis.read()) != -1) {  
  43.                     fos.write(read);  
  44.                     fos.flush();  
  45.                 }  
  46.                 fos.close();  
  47.                 fis.close();  
  48.             } catch (FileNotFoundException e) {  
  49.                 e.printStackTrace();  
  50.             } catch (IOException e) {  
  51.                 e.printStackTrace();  
  52.             }  
  53.   
  54. java文件重命名  
  55. File file = new File("D:\\123.txt");  
  56.         if(file.exists()) {  
  57.             file.renameTo(new File("D:\\1234.txt"));  
  58.         }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值