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

时间:2014-09-15 05:51:59

标签: java string file-io

我正在尝试读取文件,然后从该文件中写入特定数据。 我试图写入的数据仅指向并分割所有其他数据,即: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);
           }  
}

1 个答案:

答案 0 :(得分:1)

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