FileUtil.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package com.alvin.common.util;
  2. import java.io.*;
  3. import java.nio.charset.StandardCharsets;
  4. /**
  5. * 常用文件工具类
  6. * 1、读取所有行请使用common-io工具类
  7. * 2、读取单行数据可以使用
  8. *
  9. * @author tianyunperfect
  10. * @date 2021/01/15
  11. */
  12. public class FileUtil {
  13. /**
  14. * 获取到 BF,一定要手动关闭
  15. *
  16. * @param filePath 文件路径
  17. * @param encode 编码
  18. * @return {@link BufferedReader}* @throws FileNotFoundException 文件未发现异常
  19. */
  20. public static BufferedReader getBufferedReader(String filePath, String encode) throws FileNotFoundException, UnsupportedEncodingException {
  21. return new BufferedReader(
  22. new InputStreamReader(
  23. new FileInputStream(filePath), encode));
  24. }
  25. /**
  26. * 获取到 BF,一定要手动关闭,默认UTF-8
  27. *
  28. * @param filePath 文件路径
  29. * @return {@link BufferedReader}* @throws FileNotFoundException 文件未发现异常
  30. * @throws UnsupportedEncodingException 不支持的编码异常
  31. */
  32. public static BufferedReader getBufferedReader(String filePath) throws FileNotFoundException, UnsupportedEncodingException {
  33. return getBufferedReader(filePath, StandardCharsets.UTF_8.name());
  34. }
  35. }