简体   繁体   English

如何在JAVA文件中写入特定数据

[英]How to write a specific data in file in JAVA

I am trying to read a file then to write specific data from that file. 我试图读取一个文件,然后从该文件写入特定数据。 The data i am trying to write only points and split all other data,ie: 224.99267,147.13839 and -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 How to extract the specific data using BufferedReader and BufferedWriter classes . 我试图只写点数据并拆分所有其他数据,即: 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类提取特定数据。 The file is SVG format : 该文件是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> 

The code i tried : 我试过的代码:

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 is an XML based format, you should use an XML based parser and xPath to process it...don't reinvent the wheel, for example... 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();
}

Which outputs something like... 输出类似...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM