FileUtil.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. package api.common.util;
  2. import lombok.SneakyThrows;
  3. import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
  4. import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
  5. import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
  6. import org.springframework.util.ResourceUtils;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.*;
  9. import java.nio.MappedByteBuffer;
  10. import java.nio.channels.FileChannel;
  11. import java.nio.charset.StandardCharsets;
  12. import java.nio.file.Files;
  13. import java.nio.file.Paths;
  14. import java.util.*;
  15. public class FileUtil {
  16. /**
  17. * 获得绝对路径
  18. *
  19. * @param parentDirectoryPath 父目录
  20. * @param subStringOfFileName 文件名的子串
  21. * @return 绝对路径
  22. */
  23. @SneakyThrows
  24. public static String getAbsolutePath(String parentDirectoryPath, String subStringOfFileName) {
  25. File parentDirectoryFile = new File(parentDirectoryPath);
  26. if (!parentDirectoryFile.exists()) {
  27. throw new RuntimeException("FileUtil.getAbsolutePath() -- 目录 " + parentDirectoryPath + " 不存在!");
  28. }
  29. File[] files = parentDirectoryFile.listFiles();
  30. if (files == null || files.length == 0) {
  31. throw new RuntimeException("FileUtil.getAbsolutePath() -- 目录 " + parentDirectoryPath + " 下没有文件!");
  32. }
  33. for (File file : files) {
  34. if (file.getName().contains(subStringOfFileName)) {
  35. return file.getAbsolutePath();
  36. }
  37. }
  38. throw new RuntimeException("FileUtil.getAbsolutePath() -- 目录 " + parentDirectoryPath + " 下的文件名包含" + subStringOfFileName + " 的文件不存在!");
  39. }
  40. /**
  41. * 获取所有同一类型的文件列表。
  42. *
  43. * @param rootPath 文件 File 路径。
  44. * @param suffix 文件后缀类型。
  45. */
  46. @SneakyThrows
  47. public static List<String> listAbsolutePathByFiletype(String rootPath, String suffix) {
  48. List<String> result = new LinkedList<>();
  49. File rootFile = new File(rootPath);
  50. //1 判断文件是否存在
  51. if (!rootFile.exists()) {
  52. throw new RuntimeException("FileUtil--listAbsolutePathByFiletype 文件 " + rootPath + " 不存在!");
  53. }
  54. //2 判断是否是文件
  55. if (rootFile.isFile() && rootFile.getName().equals(suffix)) {
  56. return new ArrayList<>(Collections.singletonList(rootPath));
  57. }
  58. //3 判断是否是目录
  59. if (rootFile.isDirectory()) {
  60. File[] children = rootFile.listFiles();
  61. if (children != null) {
  62. for (File child : children) {
  63. result.addAll(listAbsolutePathByFiletype(child.getAbsolutePath(), suffix));
  64. }
  65. }
  66. }
  67. return result;
  68. }
  69. /**
  70. * 获取当前目录下的一级文件全路径列表
  71. * @param path 根路径
  72. * @return 文件全路径列表
  73. */
  74. public static List<String> ls(String path) {
  75. File[] files = new File(path).listFiles();
  76. if (files == null){
  77. throw new RuntimeException("路径不存在!");
  78. }
  79. List<String> result = new ArrayList<>();
  80. for (File file : files) {
  81. result.add(file.getAbsolutePath()) ;
  82. }
  83. return result;
  84. }
  85. @SneakyThrows
  86. public static String read(String path){
  87. return read(getFile(path));
  88. }
  89. public static String cat(String path) throws Exception {
  90. return read(getFile(path));
  91. }
  92. @SneakyThrows
  93. public static String read(File file) throws IOException {
  94. return read(Files.newInputStream(file.toPath()));
  95. }
  96. public static String read(InputStream inputStream) throws IOException {
  97. StringBuilder result = new StringBuilder();
  98. byte[] buf = new byte[4096];//创建字节数组,存储临时读取的数据
  99. int len;//记录数据读取的长度
  100. //循环读取数据
  101. while ((len = inputStream.read(buf)) != -1) { //长度为-1则读取完毕
  102. result.append(new String(buf, 0, len)).append("\n");
  103. }
  104. inputStream.close();
  105. return result.toString();
  106. }
  107. // -------------------------------- A --------------------------------
  108. /**
  109. * 添加文件后缀
  110. *
  111. * @param url 路径
  112. * @return 有后缀名的文件
  113. */
  114. public static String addSeparator(String url) {
  115. if (File.separator.equals(url.charAt(url.length() - 1) + "")) {
  116. return url;
  117. }
  118. return url + File.separator;
  119. }
  120. /**
  121. * 添加文件后缀
  122. *
  123. * @param ambiguousFileName 不确定是否有后缀的文件名
  124. * @return 有后缀名的文件
  125. */
  126. public static String addSuffix(String ambiguousFileName, String suffix) {
  127. if (ambiguousFileName == null || "".equals(ambiguousFileName)) {
  128. throw new RuntimeException("文件名参数为空!");
  129. }
  130. String[] split = ambiguousFileName.split("\\.");
  131. return split[0] + suffix;
  132. }
  133. // -------------------------------- B --------------------------------
  134. // -------------------------------- C --------------------------------
  135. /**
  136. * 检查文件
  137. * 0 代表不存在
  138. * 1 代表是文件
  139. * 2 代表是目录
  140. *
  141. * @param file 文件
  142. * @return 是否正确
  143. */
  144. public static String checkFile(File file) {
  145. if (!file.exists()) {
  146. return "0";
  147. }
  148. if (file.isFile()) {
  149. return "1";
  150. }
  151. if (file.isDirectory()) {
  152. return "2";
  153. }
  154. return null;
  155. }
  156. /**
  157. * 检查文件后缀
  158. *
  159. * @param file 文件
  160. * @param suffix 文件后缀
  161. * @return 是否正确
  162. */
  163. public static boolean checkFileType(File file, String suffix) {
  164. String fileName = file.getName();
  165. int fileNameLength = fileName.length();
  166. int suffixLength = suffix.length();
  167. if (fileNameLength < suffixLength) {
  168. return false;
  169. }
  170. return suffix.equals(fileName.substring(fileNameLength - suffixLength));
  171. }
  172. /**
  173. * 自适应转换文件大小的单位
  174. *
  175. * @param dataSize long 型的文件大小
  176. * @return 带单位的 String 类型文件大小
  177. */
  178. public static String convertUnit(long dataSize) {
  179. String dataSizeStr = null;
  180. if (dataSize >= 0 && dataSize < 1024) {
  181. dataSizeStr = dataSize + "B";
  182. } else if (dataSize >= 1024 && dataSize < 1024 * 1024) {
  183. dataSizeStr = dataSize / 1024 + "KB";
  184. } else if (dataSize >= 1024 * 1024 && dataSize < 1024 * 1024 * 1024) {
  185. dataSizeStr = dataSize / 1024 / 1024 + "MB";
  186. } else if (dataSize >= 1024 * 1024 * 1024 && dataSize < 1024 * 1024 * 1024 * 1024L) {
  187. dataSizeStr = dataSize / 1024 / 1024 / 1024 + "GB";
  188. } else if (dataSize >= 1024 * 1024 * 1024 * 1024L) {
  189. dataSizeStr = dataSize / 1024 / 1024 / 1024 / 1024 + "TB";
  190. }
  191. return dataSizeStr;
  192. }
  193. /**
  194. * 计算目录包含的文件数量
  195. *
  196. * @param directory 目录的 File 对象
  197. * @return 文件数量
  198. */
  199. public static int count(File directory) {
  200. return Objects.requireNonNull(directory.listFiles()).length;
  201. }
  202. /**
  203. * 计算目录包含的文件数量
  204. *
  205. * @param directoryPath 目录的路径
  206. * @return 文件数量
  207. */
  208. public static int count(String directoryPath) throws Exception {
  209. return count(getDirectory(directoryPath));
  210. }
  211. /**
  212. * 复制文件
  213. *
  214. * @param source 原始文件路径
  215. * @param target 目标路径
  216. * @throws Exception 异常
  217. */
  218. public static void copy(String source, String target) throws Exception {
  219. File targetFile = new File(target);
  220. boolean delete = true;
  221. if (targetFile.exists()) {
  222. delete = targetFile.delete();
  223. }
  224. if (delete) {
  225. createParentDirectory(target);
  226. Files.copy(getFile(source).toPath(), new File(target).toPath());
  227. } else {
  228. throw new Exception("删除 " + target + " 失败!");
  229. }
  230. }
  231. /**
  232. * 创建目录
  233. */
  234. public static boolean createDirectory(String path) {
  235. boolean mkdir = false;
  236. File file = new File(path);
  237. if (!file.exists()) {
  238. mkdir = file.mkdirs();
  239. }
  240. return mkdir;
  241. }
  242. /**
  243. * 创建父目录
  244. */
  245. public static boolean createParentDirectory(String path) {
  246. boolean mkdir = false;
  247. File file = new File(path);
  248. if (!file.getParentFile().exists()) {
  249. mkdir = file.getParentFile().mkdirs();
  250. }
  251. return mkdir;
  252. }
  253. /**
  254. * 创建父目录
  255. */
  256. public static boolean createParentDirectory(File file) {
  257. boolean mkdir = true;
  258. if (!file.getParentFile().exists()) {
  259. mkdir = file.getParentFile().mkdirs();
  260. }
  261. return mkdir;
  262. }
  263. /**
  264. * 创建文件。
  265. */
  266. public static boolean createFile(String path) throws IOException {
  267. File file = new File(path);
  268. boolean result = createParentDirectory(file);
  269. if (!file.exists()) {
  270. result = file.createNewFile();
  271. }
  272. return result;
  273. }
  274. /**
  275. * 创建文件。
  276. */
  277. public static boolean createFile(File file) throws IOException {
  278. boolean result = createParentDirectory(file);
  279. if (!file.exists()) {
  280. result = file.createNewFile();
  281. }
  282. return result;
  283. }
  284. /**
  285. * 拷贝文件。
  286. */
  287. public static void copyFile(String readFrom, String writeTo) throws IOException {
  288. createParentDirectory(writeTo);
  289. FileInputStream fileInputStream = new FileInputStream(readFrom);
  290. FileOutputStream fileOutputStream = new FileOutputStream(writeTo);
  291. FileChannel readChannel = fileInputStream.getChannel();
  292. FileChannel writeChannel = fileOutputStream.getChannel();
  293. //自动生成与文件大小相同但不超过 8096 的缓存,即缓存最大为 8096。
  294. writeChannel.transferFrom(readChannel, 0, readChannel.size());
  295. writeChannel.close();
  296. readChannel.close();
  297. fileOutputStream.close();
  298. fileInputStream.close();
  299. }
  300. public static void copyBytes(InputStream in, OutputStream out, int bufferSize) throws IOException {
  301. byte[] buffer = new byte[bufferSize];
  302. for (int bytesRead = in.read(buffer); bytesRead >= 0; bytesRead = in.read(buffer)) {
  303. out.write(buffer, 0, bytesRead);
  304. }
  305. }
  306. // -------------------------------- D --------------------------------
  307. /**
  308. * 供 http 下载使用
  309. *
  310. * @param fileName 文件名
  311. * @param in 输入流
  312. * @param response 响应对象
  313. * @param bufferSize 缓存大小
  314. * @throws IOException 异常
  315. */
  316. public static void downloadForHttp(String fileName, InputStream in, HttpServletResponse response, int bufferSize) throws IOException {
  317. response.setContentType("application/octet-stream;charset=utf-8");
  318. response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
  319. copyBytes(in, response.getOutputStream(), bufferSize);
  320. }
  321. public static Set<String> decompress(InputStream inputStream, String targetDirectoryPath, String fileType) throws IOException {
  322. Set<String> fileList = new HashSet<>();
  323. if (!targetDirectoryPath.endsWith("/")) {
  324. targetDirectoryPath += "/";
  325. }
  326. if (".tar".equals(fileType)) {
  327. TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(inputStream);
  328. TarArchiveEntry entry;
  329. // 将 tar 文件解压到 extractPath 目录下
  330. while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) {
  331. if (entry.isDirectory()) {
  332. continue;
  333. }
  334. createDirectory(targetDirectoryPath);
  335. File currentFile = new File(targetDirectoryPath + entry.getName());
  336. File parent = currentFile.getParentFile();
  337. if (!parent.exists()) {
  338. boolean mkdirs = parent.mkdirs();
  339. }
  340. fileList.add(currentFile.getAbsolutePath());
  341. // 将文件写出到解压的目录
  342. copyBytes(tarArchiveInputStream, new FileOutputStream(currentFile), 4096);
  343. }
  344. }
  345. if (".tar.gz".equals(fileType)|| ".tgz".equals(fileType)|| ".gz".equals(fileType)) {
  346. TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream((new GzipCompressorInputStream(inputStream)));
  347. TarArchiveEntry entry;
  348. // 将 tar 文件解压到 extractPath 目录下
  349. while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) {
  350. if (entry.isDirectory()) {
  351. continue;
  352. }
  353. createDirectory(targetDirectoryPath);
  354. File currentFile = new File(targetDirectoryPath + entry.getName());
  355. File parent = currentFile.getParentFile();
  356. if (!parent.exists()) {
  357. boolean mkdirs = parent.mkdirs();
  358. }
  359. fileList.add(currentFile.getAbsolutePath());
  360. // 将文件写出到解压的目录
  361. copyBytes(tarArchiveInputStream, new FileOutputStream(currentFile), 4096);
  362. }
  363. }
  364. inputStream.close();
  365. return fileList;
  366. }
  367. /**
  368. * 扫描压缩包中是否存在指定文件
  369. * @param inputStream
  370. * @param fileType
  371. * @return
  372. * @throws IOException
  373. */
  374. public static boolean scan(InputStream inputStream, String fileType, String targetFileName) throws IOException {
  375. if (".tar".equals(fileType)) {
  376. TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(inputStream);
  377. TarArchiveEntry entry;
  378. // 将 tar 文件解压到 extractPath 目录下
  379. while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) {
  380. if (entry.getName().contains(targetFileName)){
  381. return true;
  382. }
  383. }
  384. }
  385. if (".tar.gz".equals(fileType)|| ".tgz".equals(fileType)|| ".gz".equals(fileType)) {
  386. TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream((new GzipCompressorInputStream(inputStream)));
  387. TarArchiveEntry entry;
  388. // 将 tar 文件解压到 extractPath 目录下
  389. while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) {
  390. if (entry.getName().contains(targetFileName)){
  391. return true;
  392. }
  393. }
  394. }
  395. inputStream.close();
  396. return false;
  397. }
  398. // -------------------------------- F --------------------------------
  399. // -------------------------------- G --------------------------------
  400. /**
  401. * 获得去除文件后缀的文件名
  402. */
  403. public static List<String> getAbsolutePathList(File[] files) {
  404. List<String> absolutePathList = new ArrayList<>();
  405. for (File file : files) {
  406. absolutePathList.add(file.getAbsolutePath());
  407. }
  408. return absolutePathList;
  409. }
  410. /**
  411. * 获得目录的 File 对象
  412. */
  413. public static File getDirectory(String path) throws Exception {
  414. // 获取文件
  415. File file = new File(path);
  416. if (!file.exists()) {
  417. throw new Exception("文件" + path + "不存在!");
  418. }
  419. if (!file.isDirectory()) {
  420. throw new Exception("文件" + path + "不是目录!");
  421. }
  422. return file;
  423. }
  424. /**
  425. * 获得去除文件后缀的文件名
  426. */
  427. public static File getFile(String path) throws Exception {
  428. // 获取文件
  429. File file = new File(path);
  430. if (!file.exists()) {
  431. throw new Exception("文件" + path + "不存在!");
  432. }
  433. return file;
  434. }
  435. /**
  436. * 获得去除文件后缀的文件名
  437. */
  438. public static String getFilename(File file) {
  439. return file.getName().split("\\.")[0];
  440. }
  441. /**
  442. * 获得去除文件后缀的文件名
  443. */
  444. public static String getFilename(String path) throws Exception {
  445. return getFilename(getFile(path));
  446. }
  447. /**
  448. * 获得去除文件后缀的文件名
  449. */
  450. public static List<String> getFilenameList(File[] files) {
  451. List<String> fileNameList = new ArrayList<>();
  452. for (File file : files) {
  453. fileNameList.add(file.getName());
  454. }
  455. return fileNameList;
  456. }
  457. /**
  458. * 获取文件行数
  459. *
  460. * @param path 文件路径
  461. * @return 文件行数
  462. * @throws IOException 异常
  463. */
  464. public static long getFileLineNum(String path) throws IOException {
  465. return Files.lines(Paths.get(path)).count();
  466. }
  467. /**
  468. * 获取文件名称
  469. *
  470. * @param path 文件路径
  471. * @return 文件名
  472. */
  473. public static String getFileName(String path) {
  474. return new File(path).getName();
  475. }
  476. /**
  477. * 获取 classpath 文件
  478. *
  479. * @param relativePath 相对路径
  480. */
  481. public static File getResourceFile(String relativePath) throws IOException {
  482. return ResourceUtils.getFile("classpath:" + relativePath);
  483. }
  484. /**
  485. * 获取所有文件列表。
  486. *
  487. * @param root 文件 File 对象。
  488. */
  489. public static List<File> list(File root) throws Exception {
  490. List<File> result = new LinkedList<>();
  491. //1 判断文件是否存在
  492. if (!root.exists()) {
  493. throw new Exception("文件" + root.getAbsolutePath() + "不存在!");
  494. }
  495. //2 判断是否是文件
  496. if (root.isFile()) {
  497. return new ArrayList<>(Collections.singletonList(root));
  498. }
  499. //3 判断是否是目录
  500. if (root.isDirectory()) {
  501. File[] children = root.listFiles();
  502. if (children != null) {
  503. for (File child : children) {
  504. result.addAll(list(child));
  505. }
  506. }
  507. }
  508. return result;
  509. }
  510. /**
  511. * 获取所有文件列表。
  512. *
  513. * @param rootPath 文件 File 对象。
  514. */
  515. public static List<String> list(String rootPath) throws Exception {
  516. List<String> result = new ArrayList<>();
  517. File root = new File(rootPath);
  518. //1 判断文件是否存在
  519. if (!root.exists()) {
  520. throw new Exception("文件" + root.getAbsolutePath() + "不存在!");
  521. }
  522. //2 判断是否是文件
  523. if (root.isFile()) {
  524. return new ArrayList<>(Collections.singletonList(rootPath));
  525. }
  526. //3 判断是否是目录
  527. if (root.isDirectory()) {
  528. File[] children = root.listFiles();
  529. if (children != null) {
  530. for (File child : children) {
  531. result.addAll(list(child.getAbsolutePath()));
  532. }
  533. }
  534. }
  535. return result;
  536. }
  537. /**
  538. * 获取所有同一类型的文件列表。
  539. *
  540. * @param root 文件 File 对象。
  541. * @param suffix 文件后缀类型。
  542. */
  543. public static List<File> listMatchSuffix(File root, String suffix) throws Exception {
  544. List<File> result = new LinkedList<>();
  545. //1 判断文件是否存在
  546. if (!root.exists()) {
  547. throw new Exception("文件" + root.getAbsolutePath() + "不存在!");
  548. }
  549. //2 判断是否是文件
  550. if (root.isFile() && checkFileType(root, suffix)) {
  551. return new ArrayList<>(Collections.singletonList(root));
  552. }
  553. //3 判断是否是目录
  554. if (root.isDirectory()) {
  555. File[] children = root.listFiles();
  556. if (children != null) {
  557. for (File child : children) {
  558. result.addAll(listMatchSuffix(child, suffix));
  559. }
  560. }
  561. }
  562. return result;
  563. }
  564. /**
  565. * 获取所有同一名称的文件列表。
  566. *
  567. * @param root 文件 File 对象。
  568. * @param fullFilename 带后缀的文件名。
  569. */
  570. public static List<File> listMatchName(File root, String fullFilename) throws Exception {
  571. List<File> result = new LinkedList<>();
  572. //1 判断文件是否存在
  573. if (!root.exists()) {
  574. throw new Exception("文件" + root.getAbsolutePath() + "不存在!");
  575. }
  576. //2 判断是否是文件且文件名是否一致
  577. if (root.isFile() && fullFilename.equals(root.getName())) {
  578. return new ArrayList<>(Collections.singletonList(root));
  579. }
  580. //3 判断是否是目录
  581. if (root.isDirectory()) {
  582. File[] children = root.listFiles();
  583. if (children != null) {
  584. for (File child : children) {
  585. result.addAll(listMatchName(child, fullFilename));
  586. }
  587. }
  588. }
  589. return result;
  590. }
  591. /**
  592. * 获得父级文件。
  593. *
  594. * @return 父级文件
  595. */
  596. public static File getParentFile(String path) throws Exception {
  597. return getParentFile(getFile(path));
  598. }
  599. /**
  600. * 获得父级文件。
  601. *
  602. * @return 父级文件
  603. */
  604. public static File getParentFile(File file) {
  605. return file.getParentFile();
  606. }
  607. /**
  608. * 获得父级目录名。
  609. */
  610. public static String getParentDirectoryName(File file) {
  611. return getParentFile(file).getName().split("\\.")[0];
  612. }
  613. /**
  614. * 获得父级目录名。
  615. */
  616. public static String getParentDirectoryName(String path) throws Exception {
  617. return getParentDirectoryName(getFile(path));
  618. }
  619. /**
  620. * 获取路径下的所有文件总大小
  621. */
  622. public static double getSize(File file) throws Exception {
  623. //判断文件是否存在
  624. if (file.exists()) {
  625. //如果是目录则递归计算其内容的总大小
  626. if (file.isDirectory()) {
  627. File[] children = file.listFiles();
  628. if (children == null) {
  629. return 0;
  630. }
  631. double size = 0;
  632. for (File f : children)
  633. size += getSize(f);
  634. return size;
  635. } else {//如果是文件则直接返回其大小,以“兆”为单位
  636. return (double) file.length() / 1024 / 1024;
  637. }
  638. } else {
  639. throw new Exception("文件或者文件夹不存在,请检查路径" + file.getAbsolutePath() + "是否正确!");
  640. }
  641. }
  642. public static String getSuffix(String path) {
  643. String[] split = path.split("\\.");
  644. return split[split.length - 1];
  645. }
  646. /**
  647. * 计算文件大小,递归算法
  648. */
  649. public static long getTotalSizeOfFilesInDir(final File file) throws Exception {
  650. //判断文件是否存在
  651. if (file.exists()) {
  652. if (file.isFile()) {
  653. return file.length();
  654. }
  655. final File[] children = file.listFiles();
  656. long total = 0;
  657. if (children != null) {
  658. for (final File child : children) {
  659. total += getTotalSizeOfFilesInDir(child);
  660. }
  661. }
  662. return total;
  663. } else {
  664. throw new Exception("文件或者文件夹不存在,请检查路径" + file.getAbsolutePath() + "是否正确!");
  665. }
  666. }
  667. // -------------------------------- H --------------------------------
  668. // -------------------------------- I --------------------------------
  669. // -------------------------------- J --------------------------------
  670. // -------------------------------- K --------------------------------
  671. // -------------------------------- L --------------------------------
  672. // -------------------------------- M --------------------------------
  673. /**
  674. * 修改文件。
  675. */
  676. public static void modifyFile(String path, int offset, byte[] bytes) throws IOException {
  677. createFile(path);
  678. RandomAccessFile randomAccessFile = new RandomAccessFile(path, "rw");
  679. FileChannel channel = randomAccessFile.getChannel();
  680. MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, offset, bytes.length);
  681. mappedByteBuffer.put(bytes);
  682. channel.close();
  683. }
  684. /**
  685. * 移动文件或者重命名文件
  686. *
  687. * @param source 原始文件路径
  688. * @param target 目标路径
  689. * @return 是否操作成功
  690. * @throws Exception 异常
  691. */
  692. public static boolean mv(String source, String target) throws Exception {
  693. return getFile(source).renameTo(new File(target));
  694. }
  695. // -------------------------------- N --------------------------------
  696. // -------------------------------- O --------------------------------
  697. // -------------------------------- P --------------------------------
  698. // -------------------------------- Q --------------------------------
  699. // -------------------------------- R --------------------------------
  700. public static String readFile(String path) throws Exception {
  701. return readFile(getFile(path));
  702. }
  703. public static String readFile(File file) throws Exception {
  704. BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
  705. StringBuilder stringBuilder = new StringBuilder();
  706. String line;
  707. while ((line = reader.readLine()) != null) {
  708. stringBuilder.append(line);
  709. }
  710. reader.close();
  711. return String.valueOf(stringBuilder);
  712. }
  713. public static String readInputStream(InputStream inputStream) throws Exception {
  714. StringBuilder result = new StringBuilder();
  715. byte[] buf = new byte[1024];//创建字节数组,存储临时读取的数据
  716. int len = 0;//记录数据读取的长度
  717. //循环读取数据
  718. while ((len = inputStream.read(buf)) != -1) { //长度为-1则读取完毕
  719. result.append(new String(buf, 0, len));
  720. }
  721. inputStream.close();
  722. return result.toString();
  723. }
  724. /**
  725. * 删除文件或递归删除目录
  726. * 递归删除目录下的所有文件及子目录下所有文件
  727. */
  728. public static boolean rm(File file) {
  729. if (!file.exists()) {
  730. return false;
  731. } else {
  732. if (file.isDirectory()) {
  733. String[] children = file.list();
  734. if (children != null) {
  735. for (String child : children) {
  736. if (!rm(new File(file, child))) {
  737. return false;
  738. }
  739. }
  740. }
  741. return true;
  742. } else {
  743. return file.delete();
  744. }
  745. }
  746. }
  747. /**
  748. * 删除文件或递归删除目录
  749. * 递归删除目录下的所有文件及子目录下所有文件
  750. */
  751. public static boolean rm(String path) {
  752. return rm(new File(path));
  753. }
  754. // -------------------------------- S --------------------------------
  755. // public static String splicingPath(String... paths) {
  756. // StringBuilder result = new StringBuilder();
  757. // for (int i = 0; i < paths.length; i++) {
  758. // if (i == 0) {
  759. // if ("/".equals(paths[0].charAt(paths.length - 1) + "")) {
  760. // String substring = paths[0].substring(0, paths[0].length() - 2);
  761. // result.append(substring);
  762. // } else {
  763. // result.append(paths[0]);
  764. // }
  765. // } else {
  766. // String[] split = paths[i].split("/");
  767. //
  768. // for (String s : split) {
  769. // if (s != null && !"".equals(s)) {
  770. // result.append("/").append(s);
  771. // }
  772. // }
  773. // }
  774. // }
  775. // return result.toString();
  776. // }
  777. // -------------------------------- T --------------------------------
  778. // -------------------------------- U --------------------------------
  779. // -------------------------------- V --------------------------------
  780. // -------------------------------- W --------------------------------
  781. /**
  782. * 将字符串保存为本地文件
  783. */
  784. @SneakyThrows
  785. public static void writeStringToLocalFile(String string, String filePath) throws IOException {
  786. writeInputStreamToLocalFile(new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8)), filePath);
  787. }
  788. /**
  789. * 将字符串保存为本地文件
  790. */
  791. @SneakyThrows
  792. public static void writeStringToLocalFile(String string, String filePath, int bufferLength) throws IOException {
  793. writeInputStreamToLocalFile(new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8)), filePath, bufferLength);
  794. }
  795. /**
  796. * 将输入流保存为本地文件
  797. */
  798. public static void writeInputStreamToLocalFile(InputStream inputStream, String filePath) throws IOException {
  799. writeInputStreamToLocalFile(inputStream, filePath, 4096);
  800. }
  801. /**
  802. * 将输入流保存为本地文件
  803. */
  804. public static void writeInputStreamToLocalFile(InputStream inputStream, String filePath, int bufferLength) throws IOException {
  805. BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
  806. byte[] data = new byte[bufferLength];
  807. int dataLength;
  808. File file = new File(filePath);
  809. createParentDirectory(file);
  810. BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file, false));
  811. while ((dataLength = bufferedInputStream.read(data)) != -1) {
  812. bufferedOutputStream.write(data, 0, dataLength);
  813. }
  814. bufferedOutputStream.close();
  815. bufferedInputStream.close();
  816. }
  817. /**
  818. * 将输入流保存为本地文件
  819. */
  820. public static void writeInputStreamToLocalDirectory(InputStream inputStream, String directoryPath, String filename) throws IOException {
  821. BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
  822. byte[] data = new byte[1024];
  823. int dataLength;
  824. File file = new File(directoryPath + File.separator + filename);
  825. createDirectory(directoryPath);
  826. createFile(file);
  827. BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file, false));
  828. while ((dataLength = bufferedInputStream.read(data)) != -1) {
  829. bufferedOutputStream.write(data, 0, dataLength);
  830. }
  831. bufferedOutputStream.close();
  832. bufferedInputStream.close();
  833. }
  834. }