FileUtil.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package com.fdkankan.common.util;
  2. import java.io.*;
  3. import java.nio.ByteBuffer;
  4. import java.nio.MappedByteBuffer;
  5. import java.nio.channels.FileChannel;
  6. import java.nio.channels.FileChannel.MapMode;
  7. /**
  8. * @author MeepoGuan
  9. *
  10. * <p>Description: file_util</p>
  11. *
  12. * 2017年4月30日
  13. *
  14. */
  15. public class FileUtil {
  16. /* public static void main(String[] args) {
  17. String dirName = "d:/FH/topic/";// 创建目录
  18. FileUtil.createDir(dirName);
  19. }*/
  20. /**
  21. * 创建目录
  22. *
  23. * @param destDirName
  24. * 目标目录名
  25. * @return 目录创建成功返回true,否则返回false
  26. */
  27. public static boolean createDir(String destDirName) {
  28. File dir = new File(destDirName);
  29. if (dir.exists()) {
  30. return false;
  31. }
  32. if (!destDirName.endsWith(File.separator)) {
  33. destDirName = destDirName + File.separator;
  34. }
  35. // 创建单个目录
  36. if (dir.mkdirs()) {
  37. return true;
  38. } else {
  39. return false;
  40. }
  41. }
  42. /**
  43. * 删除文件
  44. *
  45. * @param filePathAndName
  46. * String 文件路径及名称 如c:/fqf.txt
  47. * @return boolean
  48. */
  49. public static void delFile(String filePathAndName) {
  50. try {
  51. String filePath = filePathAndName;
  52. filePath = filePath.toString();
  53. File myDelFile = new File(filePath);
  54. myDelFile.delete();
  55. } catch (Exception e) {
  56. System.out.println("删除文件操作出错");
  57. e.printStackTrace();
  58. }
  59. }
  60. /**
  61. * 读取到字节数组0
  62. *
  63. * @param filePath //路径
  64. * @throws IOException
  65. */
  66. public static byte[] getContent(String filePath) throws IOException {
  67. File file = new File(filePath);
  68. long fileSize = file.length();
  69. if (fileSize > Integer.MAX_VALUE) {
  70. System.out.println("file too big...");
  71. return null;
  72. }
  73. FileInputStream fi = new FileInputStream(file);
  74. byte[] buffer = new byte[(int) fileSize];
  75. int offset = 0;
  76. int numRead = 0;
  77. while (offset < buffer.length
  78. && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
  79. offset += numRead;
  80. }
  81. // 确保所有数据均被读取
  82. if (offset != buffer.length) {
  83. throw new IOException("Could not completely read file "
  84. + file.getName());
  85. }
  86. fi.close();
  87. return buffer;
  88. }
  89. /**
  90. * 读取到字节数组1
  91. *
  92. * @param filePath
  93. * @return
  94. * @throws IOException
  95. */
  96. public static byte[] toByteArray(String filePath) throws IOException {
  97. File f = new File(filePath);
  98. if (!f.exists()) {
  99. throw new FileNotFoundException(filePath);
  100. }
  101. ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
  102. BufferedInputStream in = null;
  103. try {
  104. in = new BufferedInputStream(new FileInputStream(f));
  105. int buf_size = 1024;
  106. byte[] buffer = new byte[buf_size];
  107. int len = 0;
  108. while (-1 != (len = in.read(buffer, 0, buf_size))) {
  109. bos.write(buffer, 0, len);
  110. }
  111. return bos.toByteArray();
  112. } catch (IOException e) {
  113. e.printStackTrace();
  114. throw e;
  115. } finally {
  116. try {
  117. in.close();
  118. } catch (IOException e) {
  119. e.printStackTrace();
  120. }
  121. bos.close();
  122. }
  123. }
  124. /**
  125. * 读取到字节数组2
  126. *
  127. * @param filePath
  128. * @return
  129. * @throws IOException
  130. */
  131. public static byte[] toByteArray2(String filePath) throws IOException {
  132. File f = new File(filePath);
  133. if (!f.exists()) {
  134. throw new FileNotFoundException(filePath);
  135. }
  136. FileChannel channel = null;
  137. FileInputStream fs = null;
  138. try {
  139. fs = new FileInputStream(f);
  140. channel = fs.getChannel();
  141. ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
  142. while ((channel.read(byteBuffer)) > 0) {
  143. // do nothing
  144. // System.out.println("reading");
  145. }
  146. return byteBuffer.array();
  147. } catch (IOException e) {
  148. e.printStackTrace();
  149. throw e;
  150. } finally {
  151. try {
  152. channel.close();
  153. } catch (IOException e) {
  154. e.printStackTrace();
  155. }
  156. try {
  157. fs.close();
  158. } catch (IOException e) {
  159. e.printStackTrace();
  160. }
  161. }
  162. }
  163. /**
  164. * Mapped File way MappedByteBuffer 可以在处理大文件时,提升性能
  165. *
  166. * @param filePath
  167. * @return
  168. * @throws IOException
  169. */
  170. public static byte[] toByteArray3(String filePath) throws IOException {
  171. FileChannel fc = null;
  172. RandomAccessFile rf = null;
  173. try {
  174. rf = new RandomAccessFile(filePath, "r");
  175. fc = rf.getChannel();
  176. MappedByteBuffer byteBuffer = fc.map(MapMode.READ_ONLY, 0,
  177. fc.size()).load();
  178. //System.out.println(byteBuffer.isLoaded());
  179. byte[] result = new byte[(int) fc.size()];
  180. if (byteBuffer.remaining() > 0) {
  181. // System.out.println("remain");
  182. byteBuffer.get(result, 0, byteBuffer.remaining());
  183. }
  184. return result;
  185. } catch (IOException e) {
  186. e.printStackTrace();
  187. throw e;
  188. } finally {
  189. try {
  190. rf.close();
  191. fc.close();
  192. } catch (IOException e) {
  193. e.printStackTrace();
  194. }
  195. }
  196. }
  197. public static File[] sort(File[] s) {
  198. //中间值
  199. File temp = null;
  200. //外循环:我认为最小的数,从0~长度-1
  201. for (int j = 0; j < s.length - 1; j++) {
  202. //最小值:假设第一个数就是最小的
  203. String min = s[j].getName();
  204. //记录最小数的下标的
  205. int minIndex = j;
  206. //内循环:拿我认为的最小的数和后面的数一个个进行比较
  207. for (int k = j + 1; k < s.length; k++) {
  208. //找到最小值
  209. if (Integer.parseInt(min.substring(0, min.indexOf("."))) > Integer.parseInt(s[k].getName().substring(0, s[k].getName().indexOf(".")))) {
  210. //修改最小
  211. min = s[k].getName();
  212. minIndex = k;
  213. }
  214. }
  215. //当退出内层循环就找到这次的最小值
  216. //交换位置
  217. temp = s[j];
  218. s[j] = s[minIndex];
  219. s[minIndex] = temp;
  220. }
  221. return s;
  222. }
  223. }