国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Java一個(gè)簡單的文件工具集

2019-11-14 22:54:21
字體:
供稿:網(wǎng)友
java一個(gè)簡單的文件工具集
  1 class FileUtils  2 {  3     //文件目錄下文件總數(shù)目  4     public static int fileNumber(File dir)  5     {  6         int filenumber = 0;  7         if(dir.exists())  8         {  9             for(File file:dir.listFiles()) 10             { 11                 if(file.isDirectory()) 12                 { 13                     filenumber = filenumber+fileNumber(file); 14                 } 15                 else 16                 { 17                     filenumber++; 18                 } 19             } 20         } 21         return filenumber; 22     } 23      24     //判斷文件是否為圖片 25     public static boolean isImage(File imgFilePath) 26     { 27         try 28         { 29             FileInputStream imgfis = new FileInputStream(imgFilePath); 30             byte []imgbyte = new byte[imgfis.available()]; 31             if((imgfis.read(imgbyte))!=-1) 32             { 33                 if(imgbyte[0] == (byte) 'G' && imgbyte[1] == (byte) 'I' && imgbyte[2] == (byte) 'F') 34                 { 35                     return true; 36                 } 37                 else if(imgbyte[1] == (byte) 'P' && imgbyte[2] == (byte) 'N' && imgbyte[3] == (byte) 'G') 38                 { 39                     return true; 40                 } 41                 else if(imgbyte[6] == (byte) 'J' && imgbyte[7] == (byte) 'F' && imgbyte[8] == (byte) 'I'&& imgbyte[9] == (byte) 'F') 42                 { 43                     return true; 44                 } 45                 else 46                 { 47                     return false; 48                 } 49             } 50         }catch(Exception e) 51         { 52             System.out.PRintln(e.toString()); 53             return false; 54         } 55         return false; 56     } 57  58  59     //返回該目錄下所有文件的文件數(shù)組 60     public static File[] listAllDirectory(File dir) 61     { 62         if(dir!=null&&dir.exists()) 63         { 64             File []finalfile = new File[fileNumber(dir)]; 65             int markfile =0; 66             int fileln=0; 67             File files[] = dir.listFiles(); 68             for(int i=0;i<files.length;i++) 69             { 70                 if(files[i].isDirectory()) 71                 { 72                     listAllDirectory(files[i]); 73                 } 74                 else 75                 { 76                     finalfile[markfile++]=files[i]; 77                 } 78             } 79             return finalfile; 80         } 81         else 82         { 83             return null; 84         } 85     } 86  87      88     //復(fù)制文件(用文件通道) 89     public static void copyFile(File oldFileAbsolutePath,File newFilePath) 90     { 91         File newFileAbsolutePath = new File(newFilePath,oldFileAbsolutePath.getName()); 92         FileInputStream fi = null; 93         FileOutputStream fo = null; 94         FileChannel in = null; 95         FileChannel out = null; 96         try { 97             fi = new FileInputStream(oldFileAbsolutePath); 98             fo = new FileOutputStream(newFileAbsolutePath); 99             in = fi.getChannel();100             out = fo.getChannel();101             in.transferTo(0, in.size(), out);102         } catch (IOException e)103             {104                 e.printStackTrace();105             }106             finally107                 {108                     try109                         {110                             fi.close();111                             in.close();112                             fo.close();113                             out.close();114                         } catch (IOException e)115                             {116                                 e.printStackTrace();117                             }118                 }119     }120 }


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 永顺县| 江津市| 潜江市| 尼勒克县| 白城市| 西乌珠穆沁旗| 泉州市| 东源县| 泰兴市| 富阳市| 铜陵市| 始兴县| 石阡县| 胶州市| 出国| 东城区| 曲沃县| 尼木县| 天水市| 五原县| 福泉市| 蒙阴县| 图木舒克市| 吴桥县| 缙云县| 胶南市| 丽江市| 尤溪县| 富源县| 永济市| 和田市| 大渡口区| 崇阳县| 安化县| 项城市| 竹溪县| 温宿县| 高青县| 上高县| 开阳县| 邳州市|