1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. import org.apache.log4j.Logger;
  12.  
  13. public class FileUtils {
  14.  
  15. /**
  16. * 读取一个文件夹下所有文件的绝对路径。忽略此文件夹下的问价夹
  17. *
  18. * @param absFolderPath
  19. * 文件夹绝对路径
  20. * @return List<String>
  21. */
  22. public static List<String> readFilePath(String absFolderPath) {
  23. List<String> paths = new ArrayList<String>();
  24. File file = new File(absFolderPath);
  25. // 文件夹
  26. if (file.isDirectory()) {
  27. for (File childFile : file.listFiles()) {
  28. // 不是文件夹并且没有被操作
  29. if (!childFile.isDirectory()) {
  30. // 路径通过"/"手动创建,避免因window下出现反斜杠\ 路径的情况
  31. paths.add(absFolderPath + "/" + childFile.getName());
  32. }
  33. }
  34. }
  35. return paths;
  36. }
  37.  
  38. /**
  39. * 通过文件的绝对路径删除文件,如果传入是文件夹路径,忽略并返回false
  40. *
  41. * @param absFilePath
  42. * 文件绝对路径
  43. * @return boolean
  44. */
  45. public static boolean deleteFile(String absFilePath) {
  46. boolean result = false;
  47. File file = new File(absFilePath);
  48. if (!file.isDirectory()) {
  49. file.delete();
  50. result = true;
  51. }
  52. return result;
  53. }
  54.  
  55. /**
  56. * 删除问价夹下所有文件
  57. *
  58. * @param absFolderPath
  59. * @return boolean
  60. */
  61. public static boolean deleteAllFileInTheFolder(String absFolderPath) {
  62. boolean result = true;
  63. for (String filePath : FileDealUtil.readFilePath(absFolderPath)) {
  64. result = result && FileDealUtil.deleteFile(filePath);
  65. }
  66. return result;
  67. }
  68.  
  69. /**
  70. * 将文件移动到指定问价夹下
  71. *
  72. * @param filePath
  73. * 文件路径
  74. * @param folderPath
  75. * 文件夹路径
  76. * @return String 移动后文件路径,filePath为文件夹或folderPath为文件或文件正在被其他进程打开无法移动返回null
  77.  
  78. */
  79. public static String moveFile(String filePath, String folderPath) {
  80. String path = null;
  81.  
  82. File file = new File(filePath);
  83. if (file.isDirectory())
  84. return path;
  85. File folder = new File(folderPath);
  86. if (!folder.exists()) {
  87. folder.mkdirs();
  88. }
  89. if (!folder.isDirectory())
  90. return path;
  91.  
  92. File nowFile = new File(folderPath + "/" + file.getName());
  93. if (nowFile.exists())
  94. nowFile.delete();
  95. if (file.renameTo(new File(folder, file.getName())))
  96. path = folderPath + "/" + file.getName();
  97. return path;
  98. }
  99.  
  100. /**
  101. * 得到文件输入流
  102. *
  103. * @param path
  104. * 绝对路径
  105. * @return InputStream
  106.  
  107. */
  108. public static InputStream getFile(String path) {
  109. // First try to read from the file system ...
  110. File file = new File(path);
  111. if (file.exists() && file.canRead()) {
  112. try {
  113. return new FileInputStream(file);
  114. } catch (FileNotFoundException e) {
  115. // continue
  116. return null;
  117. }
  118. }
  119. return null;
  120. }
  121.  
  122. /**
  123. * 判断文件夹是否存在 void
  124. *
  125. */
  126. public static boolean existFolder(String folderPath) {
  127. File file = new File(folderPath);
  128. return file.exists();
  129. }
  130.  
  131. /**
  132. * 创建文件夹
  133. *
  134. * @param folderPath
  135. * void
  136. */
  137. public static void creatFolder(String folderPath) {
  138. if (!existFolder(folderPath)) {
  139. File file = new File(folderPath);
  140. file.mkdirs();
  141. }
  142. }
  143.  
  144. /**
  145. * 获取文件大小 如果文件存在并且不是目录,返回文件大小,如果文件不存在或者是目录,返回0
  146. *
  147. * @return Long 单位bytes
  148. */
  149. public static Long getFileSize(String filePath) {
  150. File file = new File(filePath);
  151. if (file.exists() && !file.isDirectory()) {
  152. return file.length();
  153. } else {
  154. return 0L;
  155. }
  156. }
  157.  
  158. /**
  159. * 从文件路径中分离出文件名
  160. *
  161. * @param filePath
  162. * @return String
  163. */
  164. public static String splitFileName(String filePath) {
  165. return filePath.substring(filePath.lastIndexOf("/") + 1);
  166. }
  167.  
  168. /**
  169. *
  170. * @param filePath
  171. * @param inputStream
  172. * @return boolean
  173. */
  174. public static boolean writeFile(String filePath, InputStream inputStream) {
  175. OutputStream outputStream = null;
  176. try {
  177. outputStream = new FileOutputStream(filePath);
  178.  
  179. int bytesRead = 0;
  180. byte[] buffer = new byte[2048];
  181. while ((bytesRead = inputStream.read(buffer, 0, 2048)) != -1) {
  182. outputStream.write(buffer, 0, bytesRead);
  183. }
  184. return true;
  185. } catch (Exception e) {
  186. // TODO Auto-generated catch block
  187. e.printStackTrace();
  188. return false;
  189. } finally {
  190. try {
  191. outputStream.close();
  192. inputStream.close();
  193. } catch (IOException e) {
  194. // TODO Auto-generated catch block
  195. e.printStackTrace();
  196. }
  197. }
  198. }
  199. }

FileUtils的更多相关文章

  1. Java程序员的日常—— FileUtils工具类的使用

    package cn.xingoo.learn.commons; import org.apache.commons.io.FileUtils; import org.apache.commons.i ...

  2. cocos2dx的android版FileUtils的坑

    cocos2dx3.13,FileUtils-android.cpp中可以看到: FileUtils::Status FileUtilsAndroid::getContents(const std:: ...

  3. Java File类总结和FileUtils类

    Java File类总结和FileUtils类 文件存在和类型判断 创建出File类的对象并不代表该路径下有此文件或目录. 用public boolean exists()可以判断文件是否存在. Fi ...

  4. Can't exec "aclocal": No such file or directory at /usr/share/autoconf/Autom4te/FileUtils.pm line 326.

    今天执行:autoreconf -fvi的时候出现如下错误: autoreconf: Entering directory `.' autoreconf: configure.in: not usin ...

  5. 文件相关操作工具类——FileUtils.java

    文件相关操作的工具类,创建文件.删除文件.删除目录.复制.移动文件.获取文件路径.获取目录下文件个数等,满足大多数系统需求. 源码如下:(点击下载 FileUtils.java) import jav ...

  6. 应用apache FileUtils把网页另存为文件

    public static void foo() { try { URL url = new URL("http://www.webservicex.net/globalweather.as ...

  7. Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.FileUtils

    1.错误叙述性说明 警告: Could not create JarEntryRevision for [jar:file:/D:/MyEclipse/apache-tomcat-7.0.53/web ...

  8. apache的FileUtils方法大全

    FileUtils 获取系统的临时目录路径:getTempDirectoryPath() [java] view plaincopyprint? public static String getTem ...

  9. FileUtils类介绍

    Java的文件操作太基础,缺乏很多实用工具,比如对目录的操作,支持就非常的差了.如果你经常用Java操作文件或文件夹,你会觉得反复编写这些代码是令人沮丧的问题,而且要大量用到递归. 下面是的一个解决方 ...

随机推荐

  1. poj2406--Power Strings(KMP求最小循环节)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33178   Accepted: 13792 D ...

  2. [AngularJS] Sane, scalable Angular apps are tricky, but not impossible.

    Read fromhttps://medium.com/@bluepnume/sane-scalable-angular-apps-are-tricky-but-not-impossible-less ...

  3. a标签的背景图在ie8下显示问题

    今天遇到个小问题,纠结了很久,分享下 a标签添加背景图,需要给a添加display:block样式 但是在ie8下还是不能显示背景图,开始以为是由于a标签为空造成的,试了下添加内容也没用,后来注意到一 ...

  4. HTML5移动开发中的input输入框类型

    HTML5规范引入了许多新的input输入框类型 在HTML5移动开发中,通过这些新的输入框类型来显示定制后的键盘布局,用户体验更好,更容易填写各种表单 本文中,实测手机为肾4S与米4 数字类型num ...

  5. java_Collection 类集

    大体概念

  6. Android四大组件之一:ContentProvider(内容提供者)

    Android中还提供了名为ContentProvider(内容提供者),可以向其他应用提供数据,但不常用,除非是同一公司开发的App,可以向不同应用提供数据.虽然为Android的四大组件之一,但用 ...

  7. IDEA 13》》》14破解

    更新IDEA 15注册方式 http://15.idea.lanyus.com/ ------------------------------------------------ 之前的已不能用,下面 ...

  8. Nagios新添加的hosts和services有时显示,有时不显示问题解决

    在nagios配置文件hosts.cfg和services.cfg中添加了新服务器和服务列表,重启nagios服务后刷新监控页面,新添加的服务器和服务列表有时能显示出来,有时又显示不出来. 解决方法: ...

  9. SQL SERVER 中PatIndex的用法个人理解

    一般用法:PatIndex('%AAA%',‘BBBBBBBB’) 上句的意思是查找AAA在BBBBBBBB中的位置,从1开始计算,如果没有的话则返回0 其中%AAA%的用法和 SQL语句中like的 ...

  10. iOS将产品进行多语言发布,开发

    多语言就是程序的国际化.在Xcode中要实现程序的国际化,只需要简单配置,并修改相应的字符串键值对即可. 应用程序的国际化主要包括三个方面:A.程序名称国际化:B.程序内容国际化:C.程序资源国际化 ...