• Java解析E文件工具类


    1. import lombok.extern.slf4j.Slf4j;
    2. import java.io.*;
    3. import java.nio.charset.StandardCharsets;
    4. import java.util.ArrayList;
    5. import java.util.Arrays;
    6. import java.util.List;
    7. /**
    8. * @Description E文件工具类
    9. */
    10. @Slf4j
    11. public class EFileUtils {
    12. /**
    13. * 读字符串
    14. * @param text 内容
    15. * @param node 节点
    16. * @return
    17. */
    18. public static List> readText(String text, String node) {
    19. try {
    20. int startIndex = 0;
    21. int thisIndex = 0;
    22. int endIndex = 0;
    23. boolean flag = false;
    24. text = text.substring(text.indexOf("<" + node), text.indexOf(" + node) + node.length() + 2);
    25. String[] lines = text.split("\\r?\\n");
    26. List> listDatas = new ArrayList>();
    27. for (int i = 0; i < lines.length; i++) {
    28. if (flag) break;
    29. thisIndex++;
    30. if (lines[i].startsWith("<" + node)) {
    31. startIndex = thisIndex;
    32. } else if (lines[i].startsWith(" + node)) {
    33. endIndex = thisIndex;
    34. flag = true;
    35. } else if (startIndex != 0) {
    36. String[] split = lines[i].split("\\s+");
    37. List lineDatas = new ArrayList(Arrays.asList(split));
    38. lineDatas.remove(0);//
    39. listDatas.add(lineDatas);
    40. }
    41. }
    42. log.info(node + "节点标签在第" + startIndex + "-" + endIndex);
    43. return listDatas;
    44. } catch (Exception e) {
    45. log.error(e.getMessage());
    46. }
    47. return null;
    48. }
    49. /**
    50. * 读文件
    51. * path 读取的文件路径
    52. * node E文件里的节点名称
    53. */
    54. public static List> readEFile(String path, String node) {
    55. try {
    56. int startIndex = 0;
    57. int thisIndex = 0;
    58. int endIndex = 0;
    59. boolean flag = false;
    60. //文件内容的字符集 UTF8
    61. BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8));
    62. String line = reader.readLine();
    63. List> listDatas = new ArrayList>();
    64. while ((line = reader.readLine()) != null && !flag) {
    65. thisIndex++;
    66. if (line.startsWith("<" + node)) {
    67. startIndex = thisIndex;
    68. } else if (line.startsWith(" + node)) {
    69. endIndex = thisIndex;
    70. flag = true;
    71. } else if (startIndex != 0) {
    72. String[] split = line.split("\\s+");
    73. List lineDatas = new ArrayList(Arrays.asList(split));
    74. lineDatas.remove(0);//
    75. listDatas.add(lineDatas);
    76. }
    77. }
    78. log.info(node + "节点标签在第" + startIndex + "-" + endIndex);
    79. reader.close();
    80. return listDatas;
    81. } catch (IOException e) {
    82. log.error(e.getMessage());
    83. }
    84. return null;
    85. }
    86. /**
    87. * 写文件
    88. *
    89. * @param txtInfo 内容
    90. * @param filePath 文件路径
    91. * @throws IOException
    92. */
    93. public static void writeTxt(String txtInfo, String filePath) throws IOException {
    94. File file = new File(filePath);
    95. if (!file.exists()) {
    96. file.getParentFile().mkdirs();
    97. }
    98. file.createNewFile();
    99. // write 解决中文乱码问题
    100. // FileWriter fw = new FileWriter(file, true);
    101. // 写入文件的字符集 GBK 看需求而设定
    102. OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
    103. BufferedWriter bw = new BufferedWriter(fw);
    104. bw.write(txtInfo);
    105. bw.flush();
    106. bw.close();
    107. fw.close();
    108. }
    109. }

  • 相关阅读:
    【数据可视化】第四章—— 基于pandas的数据可视化(pandas基本操作)
    Java开发基础_03
    【Kafka】Java整合Kafka
    Day705.Tomcat拒绝连接原因分析及网络优化 -深入拆解 Tomcat & Jetty
    QCC51XX---Earbud双耳配对
    C++算法:给表达式添加运算符
    【无标题】
    Jmeter压力测试教程(上)
    带你区分几种并行
    【C语言】指针与数组笔试题详解
  • 原文地址:https://blog.csdn.net/guo__hang/article/details/133738471