java.io.FileNotFoundException:file:/ xxxxx

时间:2013-07-01 11:48:41

标签: java

我正在尝试从java代码编辑xml文件。这是方法:

public static void changeReportPath(String reportPath){

      DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder docBuilder;
      try {
    docBuilder = docFactory.newDocumentBuilder();
    File file = new File(reportPath);
    Document doc = docBuilder.parse(reportPath);

    // Get the root element
    Node root = doc.getFirstChild();
    // Get the staff element by tag name directly
    NodeList parameter = doc.getElementsByTagName("defaultValueExpression");

    NodeList defaultValueExpression =parameter.item(0).getChildNodes();

        defaultValueExpression.item(0).setTextContent('"'+"/home/amira/work/"+'"');

    // write the content into xml file
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);

    StreamResult result = new StreamResult(new File(reportPath));


    transformer.transform(source, result);


      } catch (ParserConfigurationException e) {

    e.printStackTrace();
      } catch (SAXException e) {

    e.printStackTrace();
      } catch (IOException e) {

    e.printStackTrace();
      } catch (TransformerConfigurationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
      } catch (TransformerException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
      }


}

这是方法调用:

 Utilities.changeReportPath("/home/amira/xx/runtime/uploads/report/project/myfile.jrxml");

这是错误日志:


java.io.FileNotFoundException: file:/home/amira/xx/runtime/uploads/report/project/DEVFACCOA.jrxml (No such file or directory)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
    at org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:235)
    at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:278)
    at com.kopimap.utilities.Utilities.changeReportPath(Utilities.java:205)
    at com.example.kopimapreportgeneration.KopimapreportgenerationUI$ReportUploader.uploadSucceeded(KopimapreportgenerationUI.java:159)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:167)
    at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:968)
    at com.vaadin.ui.Upload.fireUploadSuccess(Upload.java:811)
    at com.vaadin.ui.Upload$1.streamingFinished(Upload.java:1070)
    at com.vaadin.server.communication.FileUploadHandler.streamToReceiver(FileUploadHandler.java:554)
    at com.vaadin.server.communication.FileUploadHandler.handleFileUploadValidationAndData(FileUploadHandler.java:419)
    at com.vaadin.server.communication.FileUploadHandler.doHandleSimpleMultipartFileUpload(FileUploadHandler.java:382)
    at com.vaadin.server.communication.FileUploadHandler.handleRequest(FileUploadHandler.java:267)
    at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1329)
    at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:236)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
    at java.lang.Thread.run(Thread.java:619)

我发现了enter link description here

所以我改变了这一行:

    Document doc = docBuilder.parse(file.toURI().toString());

但问题仍然存在

2 个答案:

答案 0 :(得分:1)

使用文件而不是URI:

Document doc = docBuilder.parse(reportPath);

将其替换为:

Document doc = docBuilder.parse(file);

根据Java Doc:

  

public class FileNotFoundException extends IOException

     

表示尝试打开由指定的文件表示的文件   路径名失败。

     

FileInputStream将抛出此异常,   文件夹中的FileOutputStream和RandomAccessFile构造函数   指定的路径名​​不存在。它也会被这些抛出   构造函数如果文件存在但由于某种原因是   无法访问,例如尝试打开只读时   写作文件。

因此,您要么没有该文件,要么没有所需的权限

答案 1 :(得分:1)

我不确定,但认为你的问题是定义路径。我的意思是你应该将你的项目根路径添加到你的文件地址。