我目前正在尝试用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/java.lang.NoClassDefFoundError/XMLParseException在OfxTest.afficherFichier(OfxTest.java:31)的OfxTest.main(OfxTest.java:20)中由以下原因引起: java.lang.ClassNotFoundException: java.net.URLClassLoader$1.run(未知源)在java.security.AccessController.doPrivileged(Native方法)在java.net.URLClassLoader.findClass(未知源)在java.lang.ClassLoader.loadClass(未知源)在sun.misc.Launcher$AppClassLoader.loadClass(Unknown源)在java.lang.ClassLoader.loadClass(未知源) ... 2更多
我正在试着弄清楚。我相信它找不到XMLParseException。但我不确定。
发布于 2011-12-08 19:58:58
您遇到的第二个问题:“线程中的异常"main”java.lang.NoClassDefFoundError: net/n3/ NanoXML /XMLParseException“意味着您还没有包含此处的NanoXML库:http://devkix.com/nanoxml.php
您还需要Apache Commons日志库,因为NanoXML似乎依赖于此。可在此处获得:http://commons.apache.org/logging/download_logging.cgi
发布于 2011-08-30 04:38:51
这意味着您没有捕获到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();
}
}https://stackoverflow.com/questions/7235545
复制相似问题