文件不是使用FileOutputStream编写的

时间:2014-06-09 09:20:35

标签: java soap file-io io fileoutputstream

我有Java桌面Swing应用程序,我正在尝试编写XML文件,但它不是将数据写入文件。我的代码是:

FileOutputStream fileOutputStream = new FileOutputStream(input_file);
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
SOAPMessage soapResponse = soapConnection.call(soapcall, url);
String str = WebServiceDAO.soapMessageToString(soapResponse);
System.err.println("String*****" + str);
fileOutputStream.write(str.getBytes());
fileOutputStream.close();

我在这里获取数据,但不是在文件中写入。可能是什么问题呢?

2 个答案:

答案 0 :(得分:1)

我认为你的filePath没有解析到有效位置。尝试以下代码为您创建父文件夹。

String input_file = "D:/Temp/Sample/abc.txt" //whatever path you are using
File file = new File(input_file);
File parent_directory = file.getParentFile();
if (parent_directory != null) {
  parent_directory.mkdirs();
}
FileOutputStream out = new FileOutputStream(file);

答案 1 :(得分:0)

您也可以像这样编写XML文件

public String xmlFileWriter(String cfile,String Listname,String Nodename,String nodevalue){

    try {

        File fXmlFile = new File(cfile);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

        log.info("xml root element selected :" + doc.getDocumentElement().getNodeName());

        NodeList nList = doc.getElementsByTagName(Listname);



        for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);

            log.info("xml top level 1 element selected :" + nNode.getNodeName());

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                log.info("xml top level 2 element selected :" + Nodename);
                log.info("xml top level 2 element new value :" + nodevalue);

                eElement.getElementsByTagName(Nodename).item(0).setTextContent(nodevalue);
                log.info("After calling setattribute :" + eElement.getElementsByTagName(Nodename).item(0).getTextContent());
            }
        }

        /* to save xml file  begin */

        Source source = new DOMSource(doc);
        Result xmlresult = new StreamResult(fXmlFile);
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
     //   FileOutputStream fos = new FileOutputStream(new File("outFoFile.fo");
     //   transformer.transform(new StreamSource(xmlFile), new StreamResult(fos));
        xformer.setOutputProperty(OutputKeys.INDENT, "UTF-8");
       xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        xformer.transform(source, xmlresult);
        result2 = "Success";
        /* to save xml file  end */


    } catch (Exception e) {
        e.printStackTrace();
        log.error(e.toString());
        result2 = "Faild";
    }

    return result2;

}