FileUtils.java 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package edu.travel.elastic;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. /**
  7. * 文件操作类
  8. */
  9. @Slf4j
  10. public class FileUtils {
  11. /**
  12. * 读取项目resources文件夹下的文件
  13. *
  14. * @param filePath 文件路径
  15. * @return 文件内容
  16. */
  17. public static String readFileContent(String filePath) {
  18. try {
  19. BufferedReader reader = new BufferedReader(new FileReader(filePath));
  20. String line;
  21. StringBuilder stringBuilder = new StringBuilder();
  22. while ((line = reader.readLine()) != null) {
  23. stringBuilder.append(line);
  24. }
  25. reader.close();
  26. return stringBuilder.toString();
  27. } catch (IOException e) {
  28. log.error("readFileContent error.", e);
  29. }
  30. return null;
  31. }
  32. public static void main(String[] args) {
  33. String filePath = "src/main/resources/es/mappings_test20231216.txt";
  34. String fileContent = readFileContent(filePath);
  35. }
  36. }