博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java一个简单的文件工具集
阅读量:4597 次
发布时间:2019-06-09

本文共 2141 字,大约阅读时间需要 7 分钟。

1 class FileUtils  2 {  3     //文件目录下文件总数目  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     //返回该目录下所有文件的文件数组 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

 

转载于:https://www.cnblogs.com/laudoak/p/4276830.html

你可能感兴趣的文章
深入理解DIP、IoC、DI以及IoC容器
查看>>
赋值文件
查看>>
Vue 数组 字典 template v-for 的使用
查看>>
蓝牙模块选择经验谈
查看>>
java中==和equals
查看>>
CCActionPageTurn3D
查看>>
python random
查看>>
esp32-智能语音-cli(调试交互命令)
查看>>
netty与MQ使用心得
查看>>
关于dl dt dd 文字过长换行在移动端显示对齐的探讨总结
查看>>
swoolefy PHP的异步、并行、高性能网络通信引擎内置了Http/WebSocket服务器端/客户端...
查看>>
Python学习笔记
查看>>
unshift()与shift()
查看>>
使用 NPOI 、aspose实现execl模板公式计算
查看>>
行为型模式:中介者模式
查看>>
How to Notify Command to evaluate in mvvmlight
查看>>
33. Search in Rotated Sorted Array
查看>>
461. Hamming Distance
查看>>
Python垃圾回收机制详解
查看>>
{面试题1: 赋值运算符函数}
查看>>