gpt4 book ai didi

java - 如何用JAVA向文件中写入特定数据

转载 作者:行者123 更新时间:2023-12-01 12:32:22 25 4
gpt4 key购买 nike

我正在尝试读取一个文件,然后从该文件写入特定数据。我试图只写入点并分割所有其他数据的数据,即: 224.99267,147.13839-0.318,1.03 -0.347,1.621 -1.458,2.069 -0.616,0.249 -1.188, 0.442 -1.808,0.677 -1.297,0.49 -2.719,0.723 -4.1,0.738 l 0,0如何使用 BufferedReaderBufferedWriter 类提取特定数据。该文件是 SVG 格式:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg>
<g
inkscape:groupmode="layer"
id="layer1"
inkscape:label="Layer"
sodipodi:insensitive="true">
<path
inkscape:connector-curvature="0"
style="fill:#000000"

d="m 185.81799,189.19002 c -0.318,1.03 -0.347,1.621 -1.458,2.069 -0.616,0.249 -1.188,0.442 -1.808,0.677 -1.297,0.49 -2.719,0.723 -4.1,0.738 l 0,0 z"

id="path1" />
</g>
<g
id="layer2"
inkscape:label="m t"
transform="translate(-63.291749,38.902944)"
style="opacity:1">
<path
inkscape:connector-curvature="0"
style="fill:#00aaff;fill-opacity:1;stroke:none"

d="m 224.99267,147.13839 c 0.058,-2.71786 -4.53925,-2.92466 -6.34648,-2.40667 -1.89481,0.52556 -2.64752,1.47594 -3.33393,3.65252 -0.19989,0.60287 -0.23084,1.0276 0.49629,0.59313 z"

id="path2"/>
</g>
</svg>

我尝试过的代码:

BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
public void RWFile(){
try {
bufferedReader = new BufferedReader(new FileReader("Out\\read.svg"));
bufferedWriter = new BufferedWriter(new FileWriter("Out\\write.svg"));
String verify;
Sting strSplite;
while ((verify = bufferedReader.readLine()) != null) {
//strSplite=verify.split(verify);
bufferedWriter.write(strSplit);
bufferedWriter.newLine();
}
bufferedReader.close();
bufferedWriter.close();
System.out.println("Copying has done");
}catch(FileNotFoundException e) {
System.out.println(e);
}catch(IOException e) {
System.out.println(e);
}
}

最佳答案

SVG 是一种基于 XML 的格式,您应该使用基于 XML 的解析器和 xPath 来处理它...例如,不要重新发明轮子...

try (InputStream is = new FileInputStream("Test.svg")) {

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(is);
XPath xpath = XPathFactory.newInstance().newXPath();

// Find the "thing" node...
javax.xml.xpath.XPathExpression exp = xpath.compile("/svg/g/path[@d]");
NodeList nl = (NodeList) exp.evaluate(dom, XPathConstants.NODESET);
for (int index = 0; index < nl.getLength(); index++) {
Node node = nl.item(index);
Node dNode = node.getAttributes().getNamedItem("d");
System.out.println(dNode.getTextContent());
}

} catch (Exception exp) {
exp.printStackTrace();
}

输出类似...

m 185.81799,189.19002 c -0.318,1.03 -0.347,1.621 -1.458,2.069 -0.616,0.249 -1.188,0.442 -1.808,0.677 -1.297,0.49 -2.719,0.723 -4.1,0.738 l 0,0 z
m 224.99267,147.13839 c 0.058,-2.71786 -4.53925,-2.92466 -6.34648,-2.40667 -1.89481,0.52556 -2.64752,1.47594 -3.33393,3.65252 -0.19989,0.60287 -0.23084,1.0276 0.49629,0.59313 z

关于java - 如何用JAVA向文件中写入特定数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25841575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com