0

我目前正在尝试使用 java 读取一个 ofx 文件。但我收到以下错误:(Unhandled exception type FileNotFoundException对于第二行)。我正在使用 OFx4j。你能给我一些关于那个的建议吗?

这是我到目前为止编写的代码:

String filename=new String("C:\\file.ofx");
    FileInputStream file = new FileInputStream(filename);
    NanoXMLOFXReader nano = new NanoXMLOFXReader();

    try
    {
        nano.parse(stream);
        System.out.println("woooo It workssss!!!!");
    }
    catch (OFXParseException e)
    {
    }

感谢您的评论,我做了一些更改:

String FILE_TO_READ = "C:\\file.ofx";


    try
    {
        FileInputStream file = new FileInputStream(FILE_TO_READ);
        NanoXMLOFXReader nano = new NanoXMLOFXReader();


        nano.parse(file);
        System.out.println("woooo It workssss!!!!");
    }
    catch (OFXParseException e)
    {
        System.out.println("Message : "+e.getMessage());
    }
    catch (Exception e1)
    {
        System.out.println("Other Message : "+e1.getMessage());
    }

但现在我得到了这个:

线程“main”中的异常 java.lang.NoClassDefFoundError: net/n3/nanoxml/XMLParseException at OfxTest.afficherFichier(OfxTest.java:31) at OfxTest.main(OfxTest.java:20) 原因:java.lang.ClassNotFoundException: net.n3.nanoxml.XMLParseException at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader .loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 2 更多

我试图弄清楚。我相信它找不到 XMLParseException。但我不确定。

4

2 回答 2

4

您遇到的第二个问题:“线程“主”java.lang.NoClassDefFoundError 中的异常:net/n3/nanoxml/XMLParseException”意味着您没有从此处包含 NanoXML 库:http: //devkix.com /nanoxml.php

您还需要 Apache Commons Logging 库,因为 NanoXML 似乎依赖于此。可在此处获得:http ://commons.apache.org/logging/download_logging.cgi

于 2011-12-08T11:58:57.873 回答
1

这意味着你没有抓到FileNotFoundException。此外,尽管这与您的错误消息无关,但作为最佳实践,您应该始终在 finally 块中关闭文件流,如下所示。也不需要对文件名执行 new String() 操作。

FileNotFoundException为:-添加这个 catch 块

    String filename = "C:\\file.ofx";
    FileInputStream file = null;
    NanoXMLOFXReader nano = null;
    try
    {
         file = new FileInputStream(filename);
         nano = new NanoXMLOFXReader();
        nano.parse(stream);
        System.out.println("woooo It workssss!!!!");
    }
    catch (OFXParseException e)
    {
        e.printStackTrace();
        throw e;
    }catch (FileNotFoundException e){
        e.printStackTrace();
        throw e;
    }finally{
        if(file!=null){
           file.close();
        }
    }
于 2011-08-29T20:38:51.137 回答