FileUtils.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package com.izouma.awesomeAdmin.utils;
  2. import org.apache.commons.lang3.StringUtils;
  3. import java.io.*;
  4. import java.nio.file.Files;
  5. import java.nio.file.Path;
  6. import java.nio.file.attribute.PosixFileAttributeView;
  7. import java.nio.file.attribute.PosixFileAttributes;
  8. import java.nio.file.attribute.PosixFilePermission;
  9. import java.nio.file.attribute.PosixFilePermissions;
  10. import java.util.Set;
  11. public class FileUtils {
  12. public static String getExtension(String fileName) {
  13. if (StringUtils.INDEX_NOT_FOUND == StringUtils.indexOf(fileName, "."))
  14. return StringUtils.EMPTY;
  15. String ext = StringUtils.substring(fileName, StringUtils.lastIndexOf(fileName, "."));
  16. return StringUtils.trimToEmpty(ext);
  17. }
  18. public static String getFileName(String header) {
  19. String[] tempArr1 = header.split(";");
  20. String[] tempArr2 = tempArr1[2].split("=");
  21. // 获取文件名,兼容各种浏览器的写法
  22. return tempArr2[1].substring(tempArr2[1].lastIndexOf("\\") + 1).replaceAll("\"", "");
  23. }
  24. /**
  25. * 获取文件权限
  26. *
  27. * <pre>
  28. * 当前仅支持posix系统,需要修改为windows格式
  29. * </pre>
  30. *
  31. * @param path
  32. * @return
  33. * @throws IOException
  34. */
  35. public static String getPermissions(Path path) throws IOException {
  36. PosixFileAttributeView fileAttributeView = Files.getFileAttributeView(path, PosixFileAttributeView.class);
  37. PosixFileAttributes readAttributes = fileAttributeView.readAttributes();
  38. Set<PosixFilePermission> permissions = readAttributes.permissions();
  39. return PosixFilePermissions.toString(permissions);
  40. }
  41. /**
  42. * 修改文件权限
  43. *
  44. * @param file
  45. * @param permsCode
  46. * @param recursive
  47. * @return
  48. * @throws IOException
  49. */
  50. public static String setPermissions(File file, String permsCode, boolean recursive) throws IOException {
  51. PosixFileAttributeView fileAttributeView = Files.getFileAttributeView(file.toPath(),
  52. PosixFileAttributeView.class);
  53. fileAttributeView.setPermissions(PosixFilePermissions.fromString(permsCode));
  54. if (file.isDirectory() && recursive && file.listFiles() != null) {
  55. for (File f : file.listFiles()) {
  56. setPermissions(f, permsCode, true);
  57. }
  58. }
  59. return permsCode;
  60. }
  61. public static boolean write(InputStream inputStream, File f) {
  62. boolean ret = false;
  63. try (OutputStream outputStream = new FileOutputStream(f)) {
  64. int read;
  65. byte[] bytes = new byte[1024];
  66. while ((read = inputStream.read(bytes)) != -1) {
  67. outputStream.write(bytes, 0, read);
  68. }
  69. ret = true;
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. } finally {
  73. if (inputStream != null) {
  74. try {
  75. inputStream.close();
  76. } catch (IOException e) {
  77. e.printStackTrace();
  78. }
  79. }
  80. }
  81. return ret;
  82. }
  83. public static void mkFolder(String fileName) {
  84. File f = new File(fileName);
  85. if (!f.exists()) {
  86. f.mkdir();
  87. }
  88. }
  89. public static File mkFile(String fileName) {
  90. File f = new File(fileName);
  91. try {
  92. f.createNewFile();
  93. } catch (IOException e) {
  94. e.printStackTrace();
  95. }
  96. return f;
  97. }
  98. public static void fileProber(File dirFile) {
  99. File parentFile = dirFile.getParentFile();
  100. if (!parentFile.exists()) {
  101. // 递归寻找上级目录
  102. fileProber(parentFile);
  103. parentFile.mkdir();
  104. }
  105. }
  106. /**
  107. * 将文本文件中的内容读入到buffer中
  108. *
  109. * @param buffer buffer
  110. * @param filePath 文件路径
  111. * @throws IOException 异常
  112. * @date 2013-1-7
  113. */
  114. public static void readToBuffer(StringBuffer buffer, String filePath) throws IOException {
  115. InputStream is = new FileInputStream(filePath);
  116. String line; // 用来保存每行读取的内容
  117. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  118. line = reader.readLine(); // 读取第一行
  119. while (line != null) { // 如果 line 为空说明读完了
  120. buffer.append(line); // 将读到的内容添加到 buffer 中
  121. buffer.append("\n"); // 添加换行符
  122. line = reader.readLine(); // 读取下一行
  123. }
  124. reader.close();
  125. is.close();
  126. }
  127. /**
  128. * 读取文本文件内容
  129. *
  130. * @param filePath 文件所在路径
  131. * @return 文本内容
  132. * @throws IOException 异常
  133. * @date 2013-1-7
  134. */
  135. public static String readFile(String filePath) throws IOException {
  136. StringBuffer sb = new StringBuffer();
  137. FileUtils.readToBuffer(sb, filePath);
  138. return sb.toString();
  139. }
  140. /**
  141. * 功能:Java读取txt文件的内容
  142. * 步骤:1:先获得文件句柄
  143. * 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取
  144. * 3:读取到输入流后,需要读取生成字节流
  145. * 4:一行一行的输出。readline()。
  146. * 备注:需要考虑的是异常情况
  147. */
  148. public static String readFileToString(File file) {
  149. try {
  150. if (file.isFile() && file.exists()) { //判断文件是否存在
  151. Long filelength = file.length(); // 获取文件长度
  152. byte[] filecontent = new byte[filelength.intValue()];
  153. try {
  154. FileInputStream in = new FileInputStream(file);
  155. in.read(filecontent);
  156. in.close();
  157. } catch (FileNotFoundException e) {
  158. e.printStackTrace();
  159. } catch (IOException e) {
  160. e.printStackTrace();
  161. }
  162. String fileContentArr = new String(filecontent);
  163. return fileContentArr;// 返回文件内容,默认编码
  164. } else {
  165. System.out.println("找不到指定的文件");
  166. }
  167. } catch (Exception e) {
  168. System.out.println("读取文件内容出错");
  169. e.printStackTrace();
  170. }
  171. return null;
  172. }
  173. }