FileUtils.java 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package com.ruoyi.common.utils;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7. /**
  8. * 文件处理工具类
  9. *
  10. * @author ruoyi
  11. */
  12. public class FileUtils
  13. {
  14. /**
  15. * 输出指定文件的byte数组
  16. *
  17. * @param filename 文件
  18. * @return
  19. */
  20. public static void writeBytes(String filePath, OutputStream os) throws IOException
  21. {
  22. FileInputStream fis = null;
  23. try
  24. {
  25. File file = new File(filePath);
  26. if (!file.exists())
  27. {
  28. throw new FileNotFoundException(filePath);
  29. }
  30. fis = new FileInputStream(file);
  31. byte[] b = new byte[1024];
  32. int length;
  33. while ((length = fis.read(b)) > 0)
  34. {
  35. os.write(b, 0, length);
  36. }
  37. }
  38. catch (IOException e)
  39. {
  40. throw e;
  41. }
  42. finally
  43. {
  44. if (os != null)
  45. {
  46. try
  47. {
  48. os.close();
  49. }
  50. catch (IOException e1)
  51. {
  52. e1.printStackTrace();
  53. }
  54. }
  55. if (fis != null)
  56. {
  57. try
  58. {
  59. fis.close();
  60. }
  61. catch (IOException e1)
  62. {
  63. e1.printStackTrace();
  64. }
  65. }
  66. }
  67. }
  68. /**
  69. * @Desc 删除文件
  70. * @param filePath 文件
  71. * @return
  72. */
  73. public static boolean deleteFile(String filePath)
  74. {
  75. boolean flag = false;
  76. File file = new File(filePath);
  77. // 路径为文件且不为空则进行删除
  78. if (file.isFile() && file.exists())
  79. {
  80. file.delete();
  81. flag = true;
  82. }
  83. return flag;
  84. }
  85. }