package com.example.springbootstudy.work;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.io.File;
public class XmlParser {
public static void main(String[] args) {
try {
File inputFile = new File("D:/ideawork/springboot-study/src/main/java/com/example/springbootstudy/work/xml/3DMark.xml");
//读取xml的工具
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
//将读取的文件转化doc
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
System.out.println("Root element: " + doc.getDocumentElement().getNodeName());
//文件查找标签 可能多个
NodeList cpuScoreList = doc.getElementsByTagName("TimeSpyCustomCPUScore");
NodeList graphicsScoreList = doc.getElementsByTagName("TimeSpyCustomGraphicsScore");
//列表有没有数据
if (cpuScoreList.getLength() > 0 && graphicsScoreList.getLength() > 0) {
//获取标签值 第一个该标签的 item(0)
Node cpuScoreNode = cpuScoreList.item(0);
Node graphicsScoreNode = graphicsScoreList.item(0);
//
if (cpuScoreNode.getNodeType() == Node.ELEMENT_NODE && graphicsScoreNode.getNodeType() == Node.ELEMENT_NODE) {
//doc获取标签值Node 强转 Element
Element cpuScoreElement = (Element) cpuScoreNode;
Element graphicsScoreElement = (Element) graphicsScoreNode;
double Wgraphics=0.85;
double Sgraphics= Double.parseDouble(graphicsScoreElement.getTextContent());
double Wcpu=0.15;
double Scpu= Double.parseDouble(cpuScoreElement.getTextContent());
System.out.println("TimeSpyCustomCPUScore: " + cpuScoreElement.getTextContent());
System.out.println("TimeSpyCustomGraphicsScore: " + graphicsScoreElement.getTextContent());
Double TimeSpyscore=(Wgraphics+Wcpu)/((Wgraphics/Sgraphics)+(Wcpu/Scpu));
System.out.println("TimeSpyscore: " +TimeSpyscore);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.example.springbootstudy.work;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class LogReader {
public static void main(String[] args) {
String folderPath = "D:/ideawork/springboot-study/src/main/java/com/example/springbootstudy/work/log"; // 指定文件夹路径
String pattern = "==========d:\\nbtest\\runin\\R53-CheckR23\\R23.log=========="; // 指定要查找的内容
//找文件
File folder = new File(folderPath);
File[] files = folder.listFiles((dir, name) -> name.endsWith(".log")); // 获取文件夹下所有.log文件
if (files != null) {
for (File file : files) {
readFileForPattern(file, pattern);
}
} else {
System.out.println("无法找到指定文件夹或文件夹为空。");
}
}
//按行读取匹配数据pattern
private static void readFileForPattern(File file, String pattern) {
//字符流读取每一行
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
boolean foundPattern = false;
//读完文件数据
while ((line = reader.readLine()) != null) {
//找到数据
if (line.contains(pattern)) {
foundPattern = true;
continue;
}
//打印结束这个文件 找下一个文件
if (foundPattern) {
System.out.println("文件: " + file.getName());
System.out.println("数据: " + line);
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// private static String extractDataFromLine(String line, String pattern) {
// Pattern p = Pattern.compile(pattern + "(\\d+(?:\\.\\d+)?)");
// Matcher m = p.matcher(line);
// if (m.find()) {
// return m.group(1);
// }
// return "";
// }
//private static void readFileForPattern(File file, String pattern) {
// try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
// StringBuilder data = new StringBuilder();
// String line;
// boolean foundPattern = false;
//
// while ((line = reader.readLine()) != null) {
// if (line.contains(pattern)) {
// foundPattern = true;
// continue;
// }
//
// if (foundPattern) {
// data.append(line).append("\n");
// }
// }
//
// if (foundPattern && data.length() > 0) {
// System.out.println("文件: " + file.getName());
// System.out.println("数据:\n" + data);
// }
// } catch (IOException e) {
// e.printStackTrace();
// }