用java解析Ofx文件
我目前正在尝试用 java 读取 ofx 文件。 但我收到以下错误:未处理的异常类型 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 在 OfxTest.afficherFichier(OfxTest.java:31) 在 OfxTest.main(OfxTest.java:20) 引起原因:java.lang.ClassNotFoundException:net.n3.nanoxml.XMLParseException 在 java.net.URLClassLoader$1.run(来源未知) 在 java.security.AccessController.doPrivileged(本机方法) 在 java.net.URLClassLoader.findClass(来源未知) 在 java.lang.ClassLoader.loadClass(来源未知) 在 sun.misc.Launcher$AppClassLoader.loadClass(来源未知) 在 java.lang.ClassLoader.loadClass(来源未知) ... 2 更多
我正在尝试弄清楚。我相信它找不到 XMLParseException。但我不确定。
I am currently trying to read an ofx file with java.
But I get the following error: Unhandled exception type FileNotFoundException
(for the 2nd line). I am using OFx4j. Could you please give me some tips on that one?
Here is the code I have written so far:
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)
{
}
Thanks for your comments, I made some changes:
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());
}
But now I am getting this:
Exception in thread "main" java.lang.NoClassDefFoundError: net/n3/nanoxml/XMLParseException
at OfxTest.afficherFichier(OfxTest.java:31)
at OfxTest.main(OfxTest.java:20)
Caused by: 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 more
I am trying to figure it out. I believe it can't find the XMLParseException. But I am not sure.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您遇到的第二个问题:“Exception in thread "main" 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
The second problem that you're encountering: "Exception in thread "main" java.lang.NoClassDefFoundError: net/n3/nanoxml/XMLParseException" means that you haven't included the NanoXML library from here: http://devkix.com/nanoxml.php
You will also need the Apache Commons Logging library, as NanoXML appears to be dependent on this. Available here: http://commons.apache.org/logging/download_logging.cgi
这意味着您没有捕获
FileNotFoundException
。另外,虽然这与您的错误消息无关,但作为最佳实践,您应该始终在finally块中关闭文件流,如下所示。也不需要对文件名执行 new String() 操作。为
FileNotFoundException
添加此 catch 块:-This means that you are not catching
FileNotFoundException
. Also although this is not relevant to your error message but as best practice you should always close you file stream in the finally block like I have below. There is also no need to do to new String() on the file name either.Add this catch block for the
FileNotFoundException
:-