FILEUTILS 介绍

  1. import org.apache.commons.io.FileUtils;
  2. import org.apache.commons.io.filefilter.*;
  3. import org.apache.commons.logging.Log;
  4. import org.apache.commons.logging.LogFactory;
  5. import java.io.*;
  6. /**
  7. * 文件工具箱
  8. *
  9. * @author leizhimin 2008-12-15 13:59:16
  10. */
  11. public final class FileToolkit {
  12. private static final Log log = LogFactory.getLog(FileToolkit.class);
  13. /**
  14. * 复制文件或者目录,复制前后文件完全一样。
  15. *
  16. * @param resFilePath 源文件路径
  17. * @param distFolder    目标文件夹
  18. * @IOException 当操作发生异常时抛出
  19. */
  20. public static void copyFile(String resFilePath, String distFolder) throws IOException {
  21. File resFile = new File(resFilePath);
  22. File distFile = new File(distFolder);
  23. if (resFile.isDirectory()) {
  24. FileUtils.copyDirectoryToDirectory(resFile, distFile);
  25. } else if (resFile.isFile()) {
  26. FileUtils.copyFileToDirectory(resFile, distFile, true);
  27. }
  28. }
  29. /**
  30. * 删除一个文件或者目录
  31. *
  32. * @param targetPath 文件或者目录路径
  33. * @IOException 当操作发生异常时抛出
  34. */
  35. public static void deleteFile(String targetPath) throws IOException {
  36. File targetFile = new File(targetPath);
  37. if (targetFile.isDirectory()) {
  38. FileUtils.deleteDirectory(targetFile);
  39. } else if (targetFile.isFile()) {
  40. targetFile.delete();
  41. }
  42. }
  43. /**
  44. * 移动文件或者目录,移动前后文件完全一样,如果目标文件夹不存在则创建。
  45. *
  46. * @param resFilePath 源文件路径
  47. * @param distFolder    目标文件夹
  48. * @IOException 当操作发生异常时抛出
  49. */
  50. public static void moveFile(String resFilePath, String distFolder) throws IOException {
  51. File resFile = new File(resFilePath);
  52. File distFile = new File(distFolder);
  53. if (resFile.isDirectory()) {
  54. FileUtils.moveDirectoryToDirectory(resFile, distFile, true);
  55. } else if (resFile.isFile()) {
  56. FileUtils.moveFileToDirectory(resFile, distFile, true);
  57. }
  58. }
  59. /**
  60. * 重命名文件或文件夹
  61. *
  62. * @param resFilePath 源文件路径
  63. * @param newFileName 重命名
  64. * @return 操作成功标识
  65. */
  66. public static boolean renameFile(String resFilePath, String newFileName) {
  67. String newFilePath = StringToolkit.formatPath(StringToolkit.getParentPath(resFilePath) + "/" + newFileName);
  68. File resFile = new File(resFilePath);
  69. File newFile = new File(newFilePath);
  70. return resFile.renameTo(newFile);
  71. }
  72. /**
  73. * 读取文件或者目录的大小
  74. *
  75. * @param distFilePath 目标文件或者文件夹
  76. * @return 文件或者目录的大小,如果获取失败,则返回-1
  77. */
  78. public static long genFileSize(String distFilePath) {
  79. File distFile = new File(distFilePath);
  80. if (distFile.isFile()) {
  81. return distFile.length();
  82. } else if (distFile.isDirectory()) {
  83. return FileUtils.sizeOfDirectory(distFile);
  84. }
  85. return -1L;
  86. }
  87. /**
  88. * 判断一个文件是否存在
  89. *
  90. * @param filePath 文件路径
  91. * @return 存在返回true,否则返回false
  92. */
  93. public static boolean isExist(String filePath) {
  94. return new File(filePath).exists();
  95. }
  96. /**
  97. * 本地某个目录下的文件列表(不递归)
  98. *
  99. * @param folder ftp上的某个目录
  100. * @param suffix 文件的后缀名(比如.mov.xml)
  101. * @return 文件名称列表
  102. */
  103. public static String[] listFilebySuffix(String folder, String suffix) {
  104. IOFileFilter fileFilter1 = new SuffixFileFilter(suffix);
  105. IOFileFilter fileFilter2 = new NotFileFilter(DirectoryFileFilter.INSTANCE);
  106. FilenameFilter filenameFilter = new AndFileFilter(fileFilter1, fileFilter2);
  107. return new File(folder).list(filenameFilter);
  108. }
  109. /**
  110. * 将字符串写入指定文件(当指定的父路径中文件夹不存在时,会最大限度去创建,以保证保存成功!)
  111. *
  112. * @param res            原字符串
  113. * @param filePath 文件路径
  114. * @return 成功标记
  115. */
  116. public static boolean string2File(String res, String filePath) {
  117. boolean flag = true;
  118. BufferedReader bufferedReader = null;
  119. BufferedWriter bufferedWriter = null;
  120. try {
  121. File distFile = new File(filePath);
  122. if (!distFile.getParentFile().exists()) distFile.getParentFile().mkdirs();
  123. bufferedReader = new BufferedReader(new StringReader(res));
  124. bufferedWriter = new BufferedWriter(new FileWriter(distFile));
  125. char buf[] = new char[1024];         //字符缓冲区
  126. int len;
  127. while ((len = bufferedReader.read(buf)) != -1) {
  128. bufferedWriter.write(buf, 0, len);
  129. }
  130. bufferedWriter.flush();
  131. bufferedReader.close();
  132. bufferedWriter.close();
  133. } catch (IOException e) {
  134. flag = false;
  135. e.printStackTrace();
  136. }
  137. return flag;
  138. }
  139. }
  140. -------------------------------------------------------------------------------------------------------------
  141. import java.io.File;
  142. import java.util.ArrayList;
  143. import java.util.List;
  144. import java.util.Properties;
  145. /**
  146. * 字符串工具箱
  147. *
  148. * @author leizhimin 2008-12-15 22:40:12
  149. */
  150. public final class StringToolkit {
  151. /**
  152. * 将一个字符串的首字母改为大写或者小写
  153. *
  154. * @param srcString 源字符串
  155. * @param flag     大小写标识,ture小写,false大些
  156. * @return 改写后的新字符串
  157. */
  158. public static String toLowerCaseInitial(String srcString, boolean flag) {
  159. StringBuilder sb = new StringBuilder();
  160. if (flag) {
  161. sb.append(Character.toLowerCase(srcString.charAt(0)));
  162. } else {
  163. sb.append(Character.toUpperCase(srcString.charAt(0)));
  164. }
  165. sb.append(srcString.substring(1));
  166. return sb.toString();
  167. }
  168. /**
  169. * 将一个字符串按照句点(.)分隔,返回最后一段
  170. *
  171. * @param clazzName 源字符串
  172. * @return 句点(.)分隔后的最后一段字符串
  173. */
  174. public static String getLastName(String clazzName) {
  175. String[] ls = clazzName.split("\\.");
  176. return ls[ls.length - 1];
  177. }
  178. /**
  179. * 格式化文件路径,将其中不规范的分隔转换为标准的分隔符,并且去掉末尾的"/"符号。
  180. *
  181. * @param path 文件路径
  182. * @return 格式化后的文件路径
  183. */
  184. public static String formatPath(String path) {
  185. String reg0 = "\\\\+";
  186. String reg = "\\\\+|/+";
  187. String temp = path.trim().replaceAll(reg0, "/");
  188. temp = temp.replaceAll(reg, "/");
  189. if (temp.endsWith("/")) {
  190. temp = temp.substring(0, temp.length() - 1);
  191. }
  192. if (System.getProperty("file.separator").equals("\\")) {
  193. temp= temp.replace('/','\\');
  194. }
  195. return temp;
  196. }
  197. /**
  198. * 格式化文件路径,将其中不规范的分隔转换为标准的分隔符,并且去掉末尾的"/"符号(适用于FTP远程文件路径或者Web资源的相对路径)。
  199. *
  200. * @param path 文件路径
  201. * @return 格式化后的文件路径
  202. */
  203. public static String formatPath4Ftp(String path) {
  204. String reg0 = "\\\\+";
  205. String reg = "\\\\+|/+";
  206. String temp = path.trim().replaceAll(reg0, "/");
  207. temp = temp.replaceAll(reg, "/");
  208. if (temp.endsWith("/")) {
  209. temp = temp.substring(0, temp.length() - 1);
  210. }
  211. return temp;
  212. }
  213. public static void main(String[] args) {
  214. System.out.println(System.getProperty("file.separator"));
  215. Properties p = System.getProperties();
  216. System.out.println(formatPath("C:///\\xxxx\\\\\\\\\\///\\\\R5555555.txt"));
  217. //     List<String> result = series2List("asdf | sdf|siii|sapp|aaat| ", "\\|");
  218. //     System.out.println(result.size());
  219. //     for (String s : result) {
  220. //         System.out.println(s);
  221. //     }
  222. }
  223. /**
  224. * 获取文件父路径
  225. *
  226. * @param path 文件路径
  227. * @return 文件父路径
  228. */
  229. public static String getParentPath(String path) {
  230. return new File(path).getParent();
  231. }
  232. /**
  233. * 获取相对路径
  234. *
  235. * @param fullPath 全路径
  236. * @param rootPath 根路径
  237. * @return 相对根路径的相对路径
  238. */
  239. public static String getRelativeRootPath(String fullPath, String rootPath) {
  240. String relativeRootPath = null;
  241. String _fullPath = formatPath(fullPath);
  242. String _rootPath = formatPath(rootPath);
  243. if (_fullPath.startsWith(_rootPath)) {
  244. relativeRootPath = fullPath.substring(_rootPath.length());
  245. } else {
  246. throw new RuntimeException("要处理的两个字符串没有包含关系,处理失败!");
  247. }
  248. if (relativeRootPath == null) return null;
  249. else
  250. return formatPath(relativeRootPath);
  251. }
  252. /**
  253. * 获取当前系统换行符
  254. *
  255. * @return 系统换行符
  256. */
  257. public static String getSystemLineSeparator() {
  258. return System.getProperty("line.separator");
  259. }
  260. /**
  261. * 将用“|”分隔的字符串转换为字符串集合列表,剔除分隔后各个字符串前后的空格
  262. *
  263. * @param series 将用“|”分隔的字符串
  264. * @return 字符串集合列表
  265. */
  266. public static List<String> series2List(String series) {
  267. return series2List(series, "\\|");
  268. }
  269. /**
  270. * 将用正则表达式regex分隔的字符串转换为字符串集合列表,剔除分隔后各个字符串前后的空格
  271. *
  272. * @param series 用正则表达式分隔的字符串
  273. * @param regex 分隔串联串的正则表达式
  274. * @return 字符串集合列表
  275. */
  276. private static List<String> series2List(String series, String regex) {
  277. List<String> result = new ArrayList<String>();
  278. if (series != null && regex != null) {
  279. for (String s : series.split(regex)) {
  280. if (s.trim() != null && !s.trim().equals("")) result.add(s.trim());
  281. }
  282. }
  283. return result;
  284. }
  285. /**
  286. * @param strList 字符串集合列表
  287. * @return 通过“|”串联为一个字符串
  288. */
  289. public static String list2series(List<String> strList) {
  290. StringBuffer series = new StringBuffer();
  291. for (String s : strList) {
  292. series.append(s).append("|");
  293. }
  294. return series.toString();
  295. }
  296. /**
  297. * 将字符串的首字母转为小写
  298. *
  299. * @param resStr 源字符串
  300. * @return 首字母转为小写后的字符串
  301. */
  302. public static String firstToLowerCase(String resStr) {
  303. if (resStr == null) {
  304. return null;
  305. } else if ("".equals(resStr.trim())) {
  306. return "";
  307. } else {
  308. StringBuffer sb = new StringBuffer();
  309. Character c = resStr.charAt(0);
  310. if (Character.isLetter(c)) {
  311. if (Character.isUpperCase(c))
  312. c = Character.toLowerCase(c);
  313. sb.append(resStr);
  314. sb.setCharAt(0, c);
  315. return sb.toString();
  316. }
  317. }
  318. return resStr;
  319. }
  320. /**
  321. * 将字符串的首字母转为大写
  322. *
  323. * @param resStr 源字符串
  324. * @return 首字母转为大写后的字符串
  325. */
  326. public static String firstToUpperCase(String resStr) {
  327. if (resStr == null) {
  328. return null;
  329. } else if ("".equals(resStr.trim())) {
  330. return "";
  331. } else {
  332. StringBuffer sb = new StringBuffer();
  333. Character c = resStr.charAt(0);
  334. if (Character.isLetter(c)) {
  335. if (Character.isLowerCase(c))
  336. c = Character.toUpperCase(c);
  337. sb.append(resStr);
  338. sb.setCharAt(0, c);
  339. return sb.toString();
  340. }
  341. }
  342. return resStr;
  343. }
  344. }

FILEUTILS 介绍相关推荐

  1. [19/04/04-星期四] IO技术_CommonsIO(通用IO,别人造的轮子,FileUtils类 操作文件 IOUtilsl类 操作里边的内容 )...

    一.概念 JDK中提供的文件操作相关的类,但是功能都非常基础,进行复杂操作时需要做大量编程工作.实际开发中,往往需要 你自己动手编写相关的代码,尤其在遍历目录文件时,经常用到递归,非常繁琐. Apac ...

  2. 源代码文档生成 Doxygen介绍(转载)

    源代码文档生成 Doxygen介绍(转载) 收藏 Doxygen介绍 一.Doxygen介绍 在项目开发过程中最重要的是如何和团队中其它成员沟通,如何在项目完成后减低维护成本,随着公司的人员流动,怎样 ...

  3. day17(JDBC入门jdbcUtils工具介绍)

    day17 JDBC整体思维导图 JDBC入门 导jar包:驱动! 加载驱动类:Class.forName("类名"); 给出url.username.password,其中url ...

  4. 大数据开发之Sqoop详细介绍

    备注: 测试环境 CDH 6.3.1 Sqoop 1.4.7 文章目录 一.Sqoop概述 二.Sqoop 工具概述 三.Sqoon工具详解 3.1 codegen 3.2 create-hive-t ...

  5. python解释器cpython的源码简要介绍

    0. 参考文档 参考文档如下: Your Guide to the CPython Source Code [python]带你入门cpython源代码,让你不再一头雾水!尝试去读读python的实现 ...

  6. Android项目实战之高仿网易云音乐项目介绍

    这一节我们来讲解这个项目所用到的一些技术,以及一些实现的效果图,让大家对该项目有一个整体的认识,推荐大家收藏该文章,因为我们发布文章后会在该文章里面加入链接,这样大家找着就很方便. 目录 第1章 前期 ...

  7. 详解用Java实现爬虫:HttpClient和Jsoup的介绍及使用(请求方式、请求参数、连接池、解析获取元素)

    一.介绍:何为爬虫 网络爬虫(Web crawler)也叫做网络机器人 可以代替人 自动地在互联网中进行数据信息的采集和整理 是一种按照一定的规则 自动地抓取万维网信息的程序或者脚本 可以自动采集所有 ...

  8. TestNG开源插件Arrow介绍

    TestNG开源插件Arrow介绍 来自孔庆云kevin   2014-03-04 17:16:17|  分类: 测试工具 TestNG是一个设计用来简化广泛测试需求的 测试框架,旨在涵盖所有类型的测 ...

  9. 学生选课系统项目介绍及需求

    学生选课系统项目介绍及需求 第一天 笔记: 数据库基础知识 主键:Primary Key ,在表中唯一确定该条记录的字段 外键:Foreign Key ,引用(参照)另外一个表中的主键字段 图书表 i ...

  10. sikuli介绍及解决点击flash按钮的问题

    开始 还是继续上次的文章 解决 Selenium文件上传框无法识别的问题 在这篇文章里面介绍了如何解决windows文件上传框的问题.但是其实当时却避开了一个比较严重的问题,如何点击flash按钮.因 ...

最新文章

  1. Linux文件压缩与解压缩
  2. SpringBoot-服务端参数验证-JSR-303验证框架
  3. DRBD 高可用配置详解(转)
  4. 【蓝桥杯Java_C组·从零开始卷】第一节、环境与变量类型运算符与类型分析
  5. 《Python高效开发实战》实战演练——开发Django站点1
  6. 程序员面试金典 - 面试题 10.11. 峰与谷(排序/不排序)
  7. 关于JDK8采坑JCE加密限制版本问题
  8. 汇编入门之输入、输出、奇偶判断、多字节变量定义
  9. hive 元数据 自定义_如何在Hive中创建自定义函数UDF及如何直接通过Impala的同步元数据重用UDF的jar文件-阿里云开发者社区...
  10. WCF Transaction
  11. Selenium3笔记-WebDriver源码初探
  12. 经典测试用例--水杯测试
  13. 2019互联网月饼哪家强?阿里走情怀;百度最土豪;浪潮最高冷;抖音最创意 .........
  14. awesomes前端资源库网站
  15. java 斑马 打印不出来_通过PrintServer将原始ZPL发送到Zebra打印机不起作用
  16. android 加速度传感器测步数,基于加速度传感器的运动步数检测算法研究
  17. 中国护照含金量再上升,Qbao Network 教你玩转全球54个国家!(二)
  18. php机器代出价,直通车转化出价工具将升级为:智能出价!
  19. Swift-UICollectionView快捷创建(刨坟)
  20. 【Ubuntu 22.04】华硕主板 H110I-PLUS 网络唤醒(WOL)设置

热门文章

  1. Mac基础知识:Mac日历如何添加提醒事件的教程
  2. EasyRecovery如何恢复系统镜像
  3. Windows server 2016 部署AD(Windows 域)
  4. 每天学一点Scala之 take、takeRight、takeWhile 与 filter
  5. Linux IO系统分析(scsi篇)
  6. Ambari Server 架构
  7. Ubuntu 16 安装JDK1.8
  8. LeetCode - Two Sum
  9. Mysql指定编码导入导出数据
  10. [转]Windows server 2008网络负载均衡集群